plot line after using fprintf

5 次查看(过去 30 天)
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

采纳的回答

Asad Mirza
Asad Mirza 2019-6-4
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

更多回答(1 个)

Walter Roberson
Walter Roberson 2019-6-4
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 个评论
Elzbieta Trynkiewicz
编辑:Elzbieta Trynkiewicz 2019-6-4
Agrees, thanks for paying attention! But still it does not work.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by