Creating a function 2020a
Afficher commentaires plus anciens
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 commentaires
stozaki
le 29 Août 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
le 29 Août 2020
Modifié(e) : Chloe Walton
le 29 Août 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
Réponses (1)
Star Strider
le 29 Août 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.
Catégories
En savoir plus sur Variables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!