The graph is coming out blank
1 view (last 30 days)
Show older comments
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 Comment
Stephen23
on 16 Jun 2022
Edited: Stephen23
on 16 Jun 2022
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')
Answers (2)
David Hill
on 30 May 2022
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 Comments
Steven Lord
on 30 May 2022
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
on 30 May 2022
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')
.
See Also
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!