Euler’s method

2 Ansichten (letzte 30 Tage)
Edinson Manga
Edinson Manga am 26 Mai 2020
Bearbeitet: darova am 27 Mai 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 Kommentare
Edinson Manga
Edinson Manga am 26 Mai 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 am 26 Mai 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by