From: Ed on
The problem is: Solve the initial-value problem using the Euler method: y' - y2 = -5x
initial condition is y(0)=-1
time step is h=0.01

So I did this code which I think is really wrong. Can someone help?

% Problem: y'- y2 = -5x or y'= y2 - 5x
clear;
y0 = 1;

npoints = 50;
dt = 0.01;

y = zeros(npoints, 1); % this intitalizes the vector y to being all zeros
t = zeros(npoints, 1);

y(1)= y0; % the intial condition
t(1) = -1;

for step=1:npoints-1 %loop over the timesteps
y(step+1) = y(step) + dt*(y(step)*y(step)-5*x);
t(step+1) = t(step)+ dt;
end

plot(t,y);
ylim([-1,3]);
From: Torsten Hennig on
> The problem is: Solve the initial-value problem using
> the Euler method: y' - y2 = -5x
> initial condition is y(0)=-1
> time step is h=0.01
>
> So I did this code which I think is really wrong. Can
> someone help?
>
> % Problem: y'- y2 = -5x or y'= y2 - 5x
> clear;
> y0 = 1;
>
> npoints = 50;
> dt = 0.01;
>
> y = zeros(npoints, 1); % this intitalizes the vector
> y to being all zeros
> t = zeros(npoints, 1);
>
> y(1)= y0; % the intial condition
> t(1) = -1;
>
> for step=1:npoints-1 %loop over the timesteps
> y(step+1) = y(step) + dt*(y(step)*y(step)-5*x);
> t(step+1) = t(step)+ dt;
> end
>
> plot(t,y);
> ylim([-1,3]);

t does not appear in your differential equation.
Is x = t ?
Then you have to change
y(step+1) = y(step) + dt*(y(step)*y(step)-5*x);
to
y(step+1) = y(step) + dt*(y(step)*y(step)-5*t(step));

Best wishes
Torsten.
From: John D'Errico on
"Ed " <eduardobarrera1(a)hotmail.com> wrote in message <hr3h48$6o7$1(a)fred.mathworks.com>...
> The problem is: Solve the initial-value problem using the Euler method: y' - y2 = -5x
> initial condition is y(0)=-1
> time step is h=0.01
>
> So I did this code which I think is really wrong. Can someone help?
>
> % Problem: y'- y2 = -5x or y'= y2 - 5x

Is y2 a constant, or do you mean the square of y?

How can we know? After all, you have y0 in your
code too. Should that refer to y^0 ?


> clear;
> y0 = 1;

I could swear that you told us above that y0 was -1.

Which is true?


> npoints = 50;
> dt = 0.01;
>
> y = zeros(npoints, 1); % this intitalizes the vector y to being all zeros
> t = zeros(npoints, 1);
>
> y(1)= y0; % the intial condition
> t(1) = -1;

Why are you starting the time variable at -1?

I think you have some confusion about the problem.
You don't seem to know what is t, what does y mean.
Sit down and write out clearly what you are doing.
Think about it.

John