The graph is coming out blank

1 回表示 (過去 30 日間)
LG
LG 2022 年 5 月 30 日
編集済み: DGM 2022 年 6 月 20 日
The graph for plot(x0,y0) comes out with no values, it´s blank. How can I solve this?
clear all
clc
f=@(x,y) -y/.1; %función a resolver
x0=input('\n Ingrese el valor inicial de x0: '); % x inicial
y0=input('\n Ingrese el valor inicial de y0: '); % y inicial
xn=input('\n Ingrese el valor final de x: ');% hasta donde llega x
h=input('\n Ingrese el valor de paso h: '); %intervalo
fprintf('\n x y ');
while x0<=xn
hold on
plot(x0,y0)
fprintf('\n%4.3f %4.6f ',x0,y0); %valores de "x" y "y"
k1= f(x0,y0);
k2= f(x0+.5*h,y0+.5*h*k1);
k3= f(x0+.5*h,y0+.5*k2*h);
k4= f(x0+h,y0+k3*h);
x1=x0+h;
y1=y0+1/6*(k1+2*k2+2*k3+k4)*h;
x0=x1;
y0=y1;
end
fprintf('\n');
xspan=[0 1]
[x,y] = ode45(@(x,y) -y/.1,xspan,2); %comprobar o comparar con la función que nos da Matlab
plot(x,y)
title('funcion matlab')
  1 件のコメント
Stephen23
Stephen23 2022 年 6 月 16 日
編集済み: Stephen23 2022 年 6 月 16 日
Original question by Lizeth Armida García Valle retrieved from Google Cache:
The graph for plot(x0,y0) comes out with no values, it´s blank. How can I solve this?
clear all
clc
f=@(x,y) -y/.1; %función a resolver
x0=input('\n Ingrese el valor inicial de x0: '); % x inicial
y0=input('\n Ingrese el valor inicial de y0: '); % y inicial
xn=input('\n Ingrese el valor final de x: ');% hasta donde llega x
h=input('\n Ingrese el valor de paso h: '); %intervalo
fprintf('\n x y ');
while x0<=xn
hold on
plot(x0,y0)
fprintf('\n%4.3f %4.6f ',x0,y0); %valores de "x" y "y"
k1= f(x0,y0);
k2= f(x0+.5*h,y0+.5*h*k1);
k3= f(x0+.5*h,y0+.5*k2*h);
k4= f(x0+h,y0+k3*h);
x1=x0+h;
y1=y0+1/6*(k1+2*k2+2*k3+k4)*h;
x0=x1;
y0=y1;
end
fprintf('\n');
xspan=[0 1]
[x,y] = ode45(@(x,y) -y/.1,xspan,2); %comprobar o comparar con la función que nos da Matlab
plot(x,y)
title('funcion matlab')

サインインしてコメントする。

回答 (2 件)

David Hill
David Hill 2022 年 5 月 30 日
Index and plot after completion of loop.
f=@(x,y) -y/.1;
x=input('\n Ingrese el valor inicial de x0: ');
y=input('\n Ingrese el valor inicial de y0: ');
xn=input('\n Ingrese el valor final de x: ');
h=input('\n Ingrese el valor de paso h: ');
while x(end)<=xn
k1= f(x(end),y(end));
k2= f(x(end)+.5*h,y(end)+.5*h*k1);
k3= f(x(end)+.5*h,y(end)+.5*k2*h);
k4= f(x(end)+h,y(end)+k3*h);
x(end+1)=x(end)+h;
y(end+1)=y(end)+1/6*(k1+2*k2+2*k3+k4)*h;
end
plot(x,y);
xspan=[0 1];
  2 件のコメント
LG
LG 2022 年 5 月 30 日
Thank you!!
Steven Lord
Steven Lord 2022 年 5 月 30 日
Alternately you could create an animatedline prior to entering the loop and call addpoints on its handle inside the loop to add points to the animated line.

サインインしてコメントする。


Star Strider
Star Strider 2022 年 5 月 30 日
Try this —
f=@(x,y) -y/.1; %función a resolver
% x0=input('\n Ingrese el valor inicial de x0: '); % x inicial
% y0=input('\n Ingrese el valor inicial de y0: '); % y inicial
% xn=input('\n Ingrese el valor final de x: ');% hasta donde llega x
%
% h=input('\n Ingrese el valor de paso h: '); %intervalo
x0 = 0;
y0 = 2;
xn = 1;
h = 1E-1;
fprintf('\n x y ');
x y
figure
while x0<=xn
hold on
plot(x0,y0,'pg', 'MarkerSize',7)
fprintf('\n%4.3f %4.6f ',x0,y0); %valores de "x" y "y"
k1= f(x0,y0);
k2= f(x0+.5*h,y0+.5*h*k1);
k3= f(x0+.5*h,y0+.5*k2*h);
k4= f(x0+h,y0+k3*h);
x1=x0+h;
y1=y0+1/6*(k1+2*k2+2*k3+k4)*h;
x0=x1;
y0=y1;
end
0.000 2.000000 0.100 0.750000 0.200 0.281250 0.300 0.105469 0.400 0.039551 0.500 0.014832 0.600 0.005562 0.700 0.002086 0.800 0.000782 0.900 0.000293 1.000 0.000110
fprintf('\n');
xspan=[0 1];
[x,y] = ode45(@(x,y) -y/.1,xspan,2); %comprobar o comparar con la función que nos da Matlab
hold on
plot(x,y,'-k')
hold off
title('funcion matlab')
.
  2 件のコメント
LG
LG 2022 年 5 月 30 日
Thank you!!
Star Strider
Star Strider 2022 年 5 月 30 日
My pleasure!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeBiotech and Pharmaceutical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by