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 CenterFile Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by