Euler’s method

8 views (last 30 days)
Edinson Manga
Edinson Manga on 26 May 2020
Edited: darova on 27 May 2020
How make the map of integral curves in the region [0, 10] × [0, 10] of the differential equation y′(t) = (y (t) - 5) (cos2 (t) - 0.5) graphing simultaneously, for k = 0, 1,. . . , 10, the solution obtained using Euler's method with step h = 0.01 and with initial condition y (0) = k.
  3 Comments
Edinson Manga
Edinson Manga on 26 May 2020
Hi Darova, this is my first attempt, but I am having trouble correctly writing the function, what do you think?
Edinson Manga
Edinson Manga on 26 May 2020
H = [0.01];
Y_euler = cell(1,numel(H));
for j=1:numel(H)
h = H(j);
x = 0:h:10;
y = zeros(1, numel(x));
y(1) = 1;
n = numel(y);
for i=1:n-1
f = ((y(x)-5)*(cos(x)^2)-0.5));
y(i+1) = y(i) + h * f;
end
Y_euler{j} = [x; y].';
end
syms Y(X)
eq = diff(Y) == (Y-5)*(cos.^2)-0.5));
for i=0:10
ic = Y(0) == i;
i=i+1;
sol = dsolve(eq, ic);
y_sol = double(subs(sol, X, x));
hold on
plot(x, y_sol, 'r', 'DisplayName', 'true solution');
for k=1:numel(H)
plot(Y_euler{j}(:,1), Y_euler{j}(:,2), 'b--', 'DisplayName', ['Euler method h=' num2str(H(k), '%.3f')]);
end
end

Sign in to comment.

Accepted Answer

darova
darova on 27 May 2020
I have some suggestions
  4 Comments
darova
darova on 27 May 2020
Did it work? What about 'k'?
Edinson Manga
Edinson Manga on 27 May 2020
Edited: darova on 27 May 2020
replace that K with a For i=0:1:10
i think it works

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!