RangaKutta Method Vs Analytic Method plot problem
14 views (last 30 days)
Show older comments
x_a=linspace(0,2,n)
h=x_a(n)-x_a(n-1)
y_a=((x_a.^2+1/2.*x_a+1)).^2
y_r(1)=1;
for i=1:n-1 ;
x_r=linspace(0,2,n)
h_r=x_r(n)-x_r(n-1);
k2_x(i)=x_r(i)+h_r/2;
k1(i)=(1+4.*x_r(i)).*sqrt(y_r(i));
k2_y(i)=y_r(i)+k1(i)./2;
k2(i)=(1+4.*(k2_x(i)).*sqrt(k2_y(i)));
k3_y(i)=y_r(i)+k2(i)./2;
k3(i)=(1+4.*k2_x(i)).*sqrt(k3_y(i));
k4_x(i)=x_r(i)+h_r;
k4_y(i)=y_r(i)+k3(i);
k4(i)=(1+4.*k4_x(i)).*sqrt(k4_y(i));
y_r(i+1)=y_r(i)+h_r.*(k1./6+k4./6+k2./3+k3./3)
i=i+1;
plot(x_a,y_a,'--kd')
hold on
plot(x_r,y_r,'--y+')
end
This is program to plot graph between RangaKutta Method Vs Analytic Method.
But the dont know why the graph is not plotting.Its giving the value of y_r ,two times only.Although it should give the values n times.
0 Comments
Accepted Answer
Walter Roberson
on 3 Jul 2021
n = 128;
x_a=linspace(0,2,n)
h=x_a(n)-x_a(n-1)
y_a=((x_a.^2+1/2.*x_a+1)).^2
y_r(1)=1;
for i=1:n-1 ;
x_r=linspace(0,2,n)
h_r=x_r(n)-x_r(n-1);
k2_x(i)=x_r(i)+h_r/2;
k1(i)=(1+4.*x_r(i)).*sqrt(y_r(i));
k2_y(i)=y_r(i)+k1(i)./2;
k2(i)=(1+4.*(k2_x(i)).*sqrt(k2_y(i)));
k3_y(i)=y_r(i)+k2(i)./2;
k3(i)=(1+4.*k2_x(i)).*sqrt(k3_y(i));
k4_x(i)=x_r(i)+h_r;
k4_y(i)=y_r(i)+k3(i);
k4(i)=(1+4.*k4_x(i)).*sqrt(k4_y(i));
y_r(i+1)=y_r(i)+h_r.*(k1./6+k4./6+k2./3+k3./3)
i=i+1;
plot(x_a,y_a,'--kd')
hold on
plot(x_r,y_r(1:end-1),'--y+') %CHANGED
end
Notice in
y_r(i+1)=y_r(i)+h_r.*(k1./6+k4./6+k2./3+k3./3)
that the k* variables are vectors.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!