The graph is coming out blank

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

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')

Sign in to comment.

Answers (2)

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

Thank you!!
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.

Sign in to comment.

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')
.

Categories

Asked:

LG
on 30 May 2022

Edited:

DGM
on 20 Jun 2022

Community Treasure Hunt

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

Start Hunting!