2 views (last 30 days)
ilian dimitrov on 4 Feb 2022
Answered: Alan Stevens on 5 Feb 2022
Hello, I need help with the following Cauchy task:
The graphs in the interval of the approximate solutions obtained by the numerical method for equations of the second order with steps h1 and so on should be drawn. I tried to solve the problem in several ways but I keep getting mistakes.This is the last code I wrote:
function Euler
x0=0;
u0=-1;
Du0=0;
xmax=20;
uu=dsolve('D2u+(1/4)*Du+6*u=0','u(0)=-1','Du(0)=0','x');
s=x0:(xmax-x0)/500:xmax;
z=subs(uu,'x',s);
plot(s,z);
grid on;
hold on;
h1=0.5;
x(1)=x0
u(1)=u0
Du(1)=Du0
x=x0:h1:xmax;
for i=1:length(x)-1
D2u(i+1)+h1*(1/4)*Du(i)+h1*6*u(i)==0
end
plot(x,u,'r.');
%h2=0.4;
%h3=0.01;
end
From this code I get only the graph of the solution but not the approximations, I will be very grateful if you tell me where I'm wrong and what I need to change.

Alan Stevens on 5 Feb 2022
More like this:
% Write equations as follows:
% dudx = v u(0)=-1
% dvdx = -v/4-6u v(0)=0
%
% Then you have Euker version as:
% u(i+1) = u(i) + h1*v(i)
% v(i+1) = v(i) -h1*(v(i)/4 + 6*u(i))
x0=0;
u0=-1;
v0=0;
xmax=20;
uu=dsolve('D2u+(1/4)*Du+6*u=0','u(0)=-1','Du(0)=0','x');
Warning: Support of character vectors and strings will be removed in a future release. Use sym objects to define differential equations instead.
s=x0:(xmax-x0)/500:xmax;
z=subs(uu,'x',s);
plot(s,z);
grid on;
hold on;
h1=0.01;
x(1)=x0;
u(1)=u0;
v(1)=v0;
x=x0:h1:xmax;
for i=1:length(x)-1
u(i+1) = u(i) + h1*v(i);
v(i+1) = v(i) -h1*(v(i)/4 + 6*u(i));
end
plot(x,u,'r.');

R2020a

Community Treasure Hunt

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

Start Hunting!