finite difference method for second order ode
Ältere Kommentare anzeigen
Hi everyone. I have written this code to solve this equation: y"+2y'+y=x^2 the problem is when I put X as for example X=0:0.25:1, it gives me fairly good answers for y. but when I change X as X=0:0.1:1, the answers for y are not correct. the more I reduce the delta x, the bigger the error become. can anyone tell me what I am doing wrong? this is the code:
%y"+2y'+y=x^2;
%Boundary Conditions:y(0)=0.2, y(1)=0.8;
x=0:0.25:1 %if I change 0.25 to 0.1 the answers are not acceptable.
n=length(x);
y=zeros(1,n);
y(1,1)=0.2; y(1,n)=0.8;
y
%((y(i+1)-2y(i)+y(i-1))/h^2)+2*(y(i+1)-y(i-1)/2h+y(i)=x(i)^2;
%after simplifing: 20y(i+1)-31y(i)+12y(i-1)=x(i)^2;
%AY=B ---->A=coefficients of y, Y=y's, B=the other side of the equations.
A=zeros(n-2);
B=zeros(1,n-2);
for i=1:n-2
A(i,i)=-31;
end
for i=2:n-2
A(i,i-1)=12;
A(i-1,i)=20;
end
A %coefficient matrix
B(1,1)=x(1,2).^2-(12*y(1,1));
B(1,n-2)=x(1,n-1).^2-(20*y(1,n));
for i=2:n-3
B(1,i)=(x(1,i+1)).^2;
end
B;
BB=B' %second side of the equations;
%AX=B ---> X=A\B
X=A\BB;
XX=X';
y(1,2:n-1)=XX(1,1:n-2);
y %final answers
plot(x,y,'-*');
Thank you in advance.
4 Kommentare
madhan ravi
am 9 Sep. 2018
Why do you say it’s incorrect?
taher taher
am 9 Sep. 2018
John D'Errico
am 1 Mär. 2019
Please do not answer questions just with a question. Use comments instead. Moved to a comment:
"Hi Taher!
Did you find the problem about it ?
I am trying to solve similar problem with your codes and ı am not sure is it correct or not.
Would you share script if it is possible...
Thank you..."
Mustafa Ahmed
am 14 Mär. 2021
Write MATLAB code to solve the following BVP using forward finite difference method:
𝑢′′ +1/𝑡 𝑢′ -1/𝑡^2 𝑢 = 0 𝑢(2) = 0.008
𝑢(6.5) = 0.003
ℎ = 1.5
And plot the approximate solution and the exact solution.
help here please
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Numerical Integration and Differential Equations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
