Creating a function 2020a
古いコメントを表示
I am using the 2020a version. I have created a function and saved it as PBTask4p1_f.m
When I put in the following:
function y = PBTask4p1_f(x)
y=0.8*x.^4-13*x.^2-5*x;
end
It has this response: >> PBTask4p1_f Not enough input arguments.
Error in PBTask4p1_f
Why isn't it working?
Thank you
3 件のコメント
stozaki
2020 年 8 月 29 日
Hello Chloe,
I tried running your function in R2020a.
If you give a value to the argument x, the function printed correctly.
Did you enter a value for the argument x?
function y = PBTask4p1_f(x)
y=0.8*x.^4-13*x.^2-5*x;
end
MATLAB Command Window
>> Y = PBTask4p1_f(1)
Y =
-17.2000
>> Y = PBTask4p1_f(2)
Y =
-49.2000
Regards,
stozaki
Chloe Walton
2020 年 8 月 29 日
編集済み: Chloe Walton
2020 年 8 月 29 日
Hi,
Try creating a script like this:
I got the graph displayed correctly.
y=PBTask4p1_f(-5);
y=PBTask4p1_f(4);
x=-5:0.01:5;
y=PBTask4p1_f(x);
plot(x,y)
xlabel('Input x');
ylabel('Output y');
function y = PBTask4p1_f(x)
y=0.8*x.^4-13*x.^2-5*x;
end

Please refer to the following documents : Add Functions to Scripts
stozaki
回答 (1 件)
Star Strider
2020 年 8 月 29 日
You are not calling it correctly.
Call it (preferably from a script) as:
x = 42;
y = PBTask4p1_f(x)
and:
y =
2.4662e+006
should then appear in the calling script workspace.
カテゴリ
ヘルプ センター および File Exchange で Variables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!