plot line after using fprintf

7 views (last 30 days)
I tried to solve some equation with using Euler's formula (code is presented below) and everything is all right with one small problem. As shown below, finally I would like to plot 2 functions: 'f(x)' which is analitical solution and 'y(x)' which corresponds to Euler's solutions.
I want 'f(x)' to be plotted as continuous line (not only points). But the plot is rebelling and shows only points :(
Anybody could explains this?
dy = @(x,y)y-x^2+1; % differential equation
f = @(x)((x+1)^2)-0.5*exp(x); % analytical solution
x0 = 0; % initial point of interval
xf = 2; % final point of interval
y = 0.5; % initial condition of value of y at x0
h = 0.1; % step size for side edge
fprintf('x(i)\t\t y_Euler(i)\t\t y_analitical(i)\n') % data table header
fprintf('%f\t %f\t\t %f\n',x0,y,f(x0));
for x = x0 : h : xf-h
y = y + dy(x,y)*h; % Euler formula
x = x + h; % here we compute x as the next step
fprintf('%f\t %f\t\t %f\n',x,y,f(x)); % print results
figure(1)
plot(x,f(x),'rx-','LineWidth',2)
hold on
plot(x,y,'bo')
title('Euler`s method and exact solution')
xlabel('x','FontSize',12,'FontWeight','bold','Color','black')
ylabel('y','FontSize',12,'FontWeight','bold','Color','black')
legend('Analytical solution','Euler method')
grid on; grid minor
end

Accepted Answer

Asad Mirza
Asad Mirza on 4 Jun 2019
The issue is x, f(x), and y are all discrete points every loop. They have no vectorized connection to their previous point. One solution I can see is only plot the result afterwards and have the x, f(x), and y be vectorized.
dy = @(x,y)y-x^2+1; % differential equation
f = @(x)((x+1)^2)-0.5*exp(x); % analytical solution
x0 = 0; % initial point of interval
xf = 2; % final point of interval
y = 0.5; % initial condition of value of y at x0
h = 0.1; % step size for side edge
fprintf('x(i)\t\t y_Euler(i)\t\t y_analitical(i)\n') % data table header
fprintf('%f\t %f\t\t %f\n',x0,y,f(x0));
count=1;
for x = x0 : h : xf-h
y = y + dy(x,y)*h; % Euler formula
x = x + h; % here we compute x as the next step
fprintf('%f\t %f\t\t %f\n',x,y,f(x)); % print results
xplot(count)=x;
yplot(count)=y;
funplot(count)=f(x);
count=count+1;
end
figure(1)
plot(xplot,funplot,'rx-','LineWidth',2)
hold on
plot(xplot,yplot,'bo')
title('Euler`s method and exact solution')
xlabel('x','FontSize',12,'FontWeight','bold','Color','black')
ylabel('y','FontSize',12,'FontWeight','bold','Color','black')
legend('Analytical solution','Euler method')
grid on; grid minor
untitled.jpg

More Answers (1)

Walter Roberson
Walter Roberson on 4 Jun 2019
At any one time, x is scalar, and f(x) is scalar. So at any one time, you are asking to plot() a pair of scalars. When you ask to plot scalars, MATLAB creates a point there that is not joined to anything.
In order to get what you want, you need to record the x and f(x) values somewhere, and move the plotting to after the loop when you have the values available.
Hint:
xvals = x0 : h : xf-h;
numx = length(xvals);
fx = zeros(1, numx);
for xidx = 1 : numx
x = xvals(xidx);
fx(xidx) = f(x);
end
  1 Comment
Elzbieta Trynkiewicz
Elzbieta Trynkiewicz on 4 Jun 2019
Edited: Elzbieta Trynkiewicz on 4 Jun 2019
Agrees, thanks for paying attention! But still it does not work.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots 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!