Second order equations. Cauchy task

조회 수: 5 (최근 30일)
ilian dimitrov
ilian dimitrov 2022년 2월 4일
답변: Alan Stevens 2022년 2월 5일
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
Alan Stevens 2022년 2월 5일
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.');

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by