MATLAB: How to use Euler method with IVP with function of x and y

eulerhelphomeworkivpMATLAB

Hi, I am working on a class project and need help using Matlab as I have no experience with it. For the project I am supposed to do a Euler approximation of a function, 3x^2+y, and y(0)=1. Step size is .01, supposed to get to x=2. My code so far is:
h=0.01;
N=200;
y(1)=1;
for n=1:N
y(n+1)= y(n)+h*((3x^2)+y(n));
x(n+1)=n*h;
end
plot(x,y)
But I keep getting the error: Error: Unexpected MATLAB expression Help? Please?

Best Answer

3x is not a valid MATLAB expression. You need to have the multiplication operator between the 3 and the x. Also, you need to use only the current value of x in the expression, not the entire x vector. E.g.,
y(n+1)= y(n)+h*((3*x(n)^2)+y(n));
You will also need to initialize the first value of x(1):
x(1) = 0;
This all assumes, of course, that you have an ODE of the following form (you don't explicitly state this in your post)
dy/dx = 3x^2+y