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일

1 개 추천

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일

0 개 추천

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일
편집: Elzbieta Trynkiewicz 2019년 6월 4일
Agrees, thanks for paying attention! But still it does not work.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by