What is wrong with this matlab code?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 5K views
Firben
Messages
141
Reaction score
0
Write a user-defined MATLAB function for the following math function:
y(t) = -0.2x^4 + e^(-0.5x)*x^3+7x^2
The input to the function is x and the output is y. Write the function such that x can be a vector

Use this function to make a plot of the function y(x) -3≤x≤4

function y=chp7one(x)
y=-0.2*x.^4+exp(-0.5*x)*x.^3 + 7*x.^2;
x=[-3 -2 -1 0 1 2 3 4];
plot(x,y);

Error in ==> chp7one at 2
y=-0.2*x.^4+exp(-0.5*x)*x.^3 + 7*x.^2;
 
Last edited:
Physics news on Phys.org
I'd try it one term at a time to see which one doesn't like. I'm suspicious of the second term between the exp(...)* x shouldn't that be exp(...).* x

Just a thought I use freemat not matlab.
 
Yes, but i still get error messages
 
Firben said:
Write a user-defined MATLAB function for the following math function:
y(t) = -0.2x^4 + e^(-0.5x)*x^3+7x^2
The input to the function is x and the output is y. Write the function such that x can be a vector

Use this function to make a plot of the function y(x) -3≤x≤4

function y=chp7one(x)
y=-0.2*x.^4+exp(-0.5*x)*x.^3 + 7*x.^2;
x=[-3 -2 -1 0 1 2 3 4];
plot(x,y);

Error in ==> chp7one at 2
y=-0.2*x.^4+exp(-0.5*x)*x.^3 + 7*x.^2;

I have a couple of questions:
How can you calculate the y values before you set the x array?
Why are you setting x in your chp7one function when it is being passed in as a parameter?