# Homework Help: What is wrong with this matlab code?

1. Feb 9, 2012

### Firben

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: Feb 9, 2012
2. Feb 10, 2012

### Firben

Any idea ? (Urgent)

3. Feb 10, 2012

### Staff: Mentor

I'd try it one term at a time to see which one doesnt 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.

4. Feb 10, 2012

### Firben

Yes, but i still get error messages

5. Feb 10, 2012

### Staff: Mentor

try this, it worked on my version:

y=-0.2.*x.^4+exp(-0.5.*x).*x.^3 + 7.*x.^2

just added a few more dots to the mix

6. Feb 10, 2012

### Staff: Mentor

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?