RangaKutta Method Vs Analytic Method plot problem

14 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2021-7-3
n = 128;
x_a=linspace(0,2,n)
x_a = 1×128
0 0.0157 0.0315 0.0472 0.0630 0.0787 0.0945 0.1102 0.1260 0.1417 0.1575 0.1732 0.1890 0.2047 0.2205 0.2362 0.2520 0.2677 0.2835 0.2992 0.3150 0.3307 0.3465 0.3622 0.3780 0.3937 0.4094 0.4252 0.4409 0.4567
h=x_a(n)-x_a(n-1)
h = 0.0157
y_a=((x_a.^2+1/2.*x_a+1)).^2
y_a = 1×128
1.0000 1.0163 1.0338 1.0524 1.0722 1.0932 1.1155 1.1391 1.1639 1.1902 1.2178 1.2468 1.2774 1.3094 1.3429 1.3781 1.4148 1.4533 1.4935 1.5355 1.5792 1.6249 1.6725 1.7221 1.7738 1.8275 1.8834 1.9415 2.0020 2.0647
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
x_r = 1×128
0 0.0157 0.0315 0.0472 0.0630 0.0787 0.0945 0.1102 0.1260 0.1417 0.1575 0.1732 0.1890 0.2047 0.2205 0.2362 0.2520 0.2677 0.2835 0.2992 0.3150 0.3307 0.3465 0.3622 0.3780 0.3937 0.4094 0.4252 0.4409 0.4567
y_r = 1×2
1.0000 1.0190
x_r = 1×128
0 0.0157 0.0315 0.0472 0.0630 0.0787 0.0945 0.1102 0.1260 0.1417 0.1575 0.1732 0.1890 0.2047 0.2205 0.2362 0.2520 0.2677 0.2835 0.2992 0.3150 0.3307 0.3465 0.3622 0.3780 0.3937 0.4094 0.4252 0.4409 0.4567
Unable to perform assignment because the left and right sides have a different number of elements.
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 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by