RK fourth order method for a 2nd order differential equation
1 view (last 30 days)
Show older comments
d^2y/dx^2 + 0.6*dy/dx 0.8y = 0
parameters: y(0)=4 and y'(0)=0. from x=0 to x=5 with step size; h =0.5.
I have this 2nd order ODE which I need to solve use RK 4th order method:
But I also need to calculate value of each state variable at a different point of x = 2, using h values
of h=(0.5)^p, where p = [1, 2, 3, 4, 5, 6, 7]. and then plot(on a log-log scale) the relative error for each state variable at x = 2 as a function of h and then use that solution for each state variable at x = 2 using h = (0.5)^20 as the true solution.
I have posted my code for the RK fourth order but confused on how to go about this part. Kindly put me through. Thanks
% Using the RK 4th order to solve equation d^2y/dx^2 + 0.6*dy/dx 0.8y = 0
% parameters: y(0)=4 and y'(0)=0. from x=0 to x=5 with step size; h =0.5.
h=0.5;
%Initial conditions
x(1)=0;
y(1)=4;
z(1)=0;
N = 10;
for i=1:N
x(i+1)=x(i)+h;
k1=h*(z(i));
k11=h*(-0.6*z(i)-8*y(i));
k2=h*(z(i)+k11/2);
k12=h*(-0.6*(z(i)+k11/2)-8*(y(i)+k1/2));
k3=h*(z(i)+k12/2);
k13=h*(-0.6*(z(i)+k12/2)-8*(y(i)+k2/2));
k4=h*(z(i)+k13);
k14=h*(-0.6*(z(i)+k13)-8*(y(i)+k3));
y(i+1)=y(i)+(1/6)*(k1+2*k2+2*k3+k4);
z(i+1)=z(i)+(1/6)*(k11+2*k12+2*k13+k14);
end
x;
y;
plot(x,y)
0 Comments
Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!