Matlab % Error for Euler's Method

Click For Summary
SUMMARY

This discussion focuses on calculating the percent error in Euler's method using MATLAB. The user implements the 'eulode' function to compute numerical solutions for three different step sizes: 0.5, 0.1, and 0.01. The analytical solution is derived using the 'dsolve' function, and the percent error is calculated by comparing the analytical results with the numerical outputs. The user seeks a more efficient way to input matrices for the calculations, indicating a need for optimization in their MATLAB code.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of numerical methods, specifically Euler's method
  • Knowledge of symbolic computation in MATLAB using 'dsolve'
  • Ability to manipulate matrices and perform element-wise operations in MATLAB
NEXT STEPS
  • Optimize matrix input methods in MATLAB for numerical computations
  • Explore the 'ode45' function in MATLAB for solving ordinary differential equations
  • Learn about error analysis in numerical methods
  • Investigate vectorization techniques in MATLAB to improve code efficiency
USEFUL FOR

Students and professionals in mathematics, engineering, and computer science who are working with numerical methods and MATLAB for solving differential equations and analyzing computational errors.

John31
Messages
5
Reaction score
0

Homework Statement


I am trying to find the percent error in Euler's method with 3 different step sizes.



The Attempt at a Solution



Below is some coding I have calculating percent error of Euler's method however there has to be a more efficient way to input the matrices, I have found the first two step size errors by manually inputing the values but before I do the third (extremely long), there has to be a faster way. Any suggestions?

Code:
%% Analytical
simplify(dsolve('Dy=-x/y','y(0)=5','x'))
%% Numerical
f=@(x) (-x^2+25)^(1/2)
dydx=@(x,y) -(x/y);
[x1,y1]=eulode(dydx, [0 5],5,.5);
[x2,y2]=eulode(dydx,[0 5],5,.1);
[x3,y3]=eulode(dydx,[0 5],5,.01);
disp([x1,y1])
disp([x2,y2])
disp([x3,y3])
%% Percent Error
x1=0:.5:5;
x2=0:.1:5;
x3=0:.01:5;
analytical_step1= (-x1.^2+25).^(1/2)
analytical_step2=(-x2.^2+25).^(1/2);
analytical_step3=(-x3.^2+25).^(1/2);
numerical_1=[5.000 5.000  4.9500 4.8490 4.6943 4.4813 4.2024 3.8454 3.3903 2.8004 1.9970 ]
numerical_2=[5.0000 5.0000 4.9980 4.9940 4.9880 4.9800 4.9699 4.9579 4.9437 4.9276 4.9093 4.8889 4.8664 4.8418 4.8149 4.7858 4.7545 4.7208 4.6848 4.6464 4.6055 4.5621 4.5161 4.4673 4.4159 4.3615 4.3042 4.2438 4.1802 4.1132 4.0427 3.9685 3.8904 3.8081 3.7214 3.6301 3.5337 3.4318 3.3240 3.2096 3.0881 2.9586 2.8200 2.6711 2.5101 2.3348 2.1421 1.9273 1.6835 1.3984 1.0480];    
Percent_Error1=abs((analytical_step1-numerical_1)/analytical_step1)*100%answer displayed in percent
Percent_Error2=abs((analytical_step2-numerical_2)/analytical_step2)*100%answer displayed in percent
 
Physics news on Phys.org
Unless I'm misunderstanding the code, why isn't y1 just the transpose of numerical_1, y2 the transpose of numerical_2, etc?
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K