Creating a function 2020a
Mostra commenti meno recenti
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 Commenti
stozaki
il 29 Ago 2020
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
il 29 Ago 2020
Modificato: Chloe Walton
il 29 Ago 2020
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
Risposte (1)
Star Strider
il 29 Ago 2020
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.
Categorie
Scopri di più su Variables in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!