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 Commenti

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
Chloe Walton il 29 Ago 2020
Modificato: Chloe Walton il 29 Ago 2020
Hi Stozaki,
what do you mean give a value to the argument x? i put in exactly what you did above and it still said there was an error. on my notes it says: The function should work for x being a scalar or a vector. they dont tell me to make x= something.
i am meant to create a function file and put this in: (which is what i have done, and saved it as PBTask4p1_f.m)
function y = PBTask4p1_f(x)
y=0.8*x.^4-13*x.^2-5*x;
end
and then put this in a script: i am able to plot the graph on a script but the f(-5) etc doesnt work
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');
how did you make your work? it looks the same as mine.
Thank you
stozaki
stozaki il 29 Ago 2020
Modificato: stozaki 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

Accedi per commentare.

Risposte (1)

Star Strider
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

Richiesto:

il 29 Ago 2020

Modificato:

il 29 Ago 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by