The graph is coming out blank
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
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
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
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 ');
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
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')
.
참고 항목
카테고리
Help Center 및 File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!