Help with Euler's Method in Matlab

In summary, the person is trying to write a script to approximate the values of a differential equation and compare them to the exact solution. They have trouble with the table of values and the first time they use Matlab. Someone helped them and they were able to approximate the values and calculate the error.
  • #1
Deathcrush
40
0

Homework Statement


Well, I'm having problems with this script, I'm taking my first course on differential equations, and I am trying to write a script that approximates the values of the solution of a differential equation, and compares them to the exact solution using a table of values, it must also calculate the error, the problem is that I cannot make the table to appear using a column for each variable, instead, the values are spread horizontally and everything is pretty messed up, Its the first time I use Matlab and I might have a lot more mistakes, can anyone help me?


Homework Equations


the equations are pretty simple, they are in the bottom of the script I wrote


The Attempt at a Solution


this is what I wrote

function [t P] = Euler(t0,P0,L,N) % Nombrando la función y estableciendo los datos necesarios

Close all;
h=L./N; % estableciendo tamaño de paso
t=[]; P=[]; % t y P son vectores vacíos
t(1)=t0+h;
P(1)=P0+h.*f(t0, P0); % Estableciendo los primeros componentes de t y P

for n=1:N-1
t(n+1)=t(n)+h;
P(n+1)=P(n)+(h.*(f(t(n), P(n))));
end

Paprox=P;
Pexacta= SolucionExacta(t);
error=abs(Pexacta-Paprox);

% Para imprimir la tabla

fprintf(' t Paproximada Pexacta error \n');
fprintf('---------------------------------------------------------------------------------\n');
fprintf( '%10.5f %10.5f %10.5f %10.5f', t ,Paprox, Pexacta, error);

% Para graficar

plot(t,P);
hold on
plot(t,Pexacta);
figure;
plot(t, error);

end
function ret=f(t,P)%para insertar la ecuación diferencial que deseamos resolver por aproximación

ret=(0.0225.*P-(0.0003.*(P.^2)));
end

function f=SolucionExacta(t) % Para establecer la solución aproximada y hacer comparación

f=(0.5625.*exp(0.0225.*t))./(0.015+(0.0075.*exp(0.0225.*t)));

end
 
Physics news on Phys.org
  • #2
The issue is you're printing arrays. You need to write your print statement in a loop, looping your values.
 
  • #3
As viscousflow said you'll need a for loop to print the array. Might help to add a 'next line' command into your code \n

Here:

fprintf( '%10.5f %10.5f %10.5f %10.5f \n', t ,Paprox, Pexacta, error);
 
  • #4
It worked!, thanks a lot
 
  • #5


I would suggest breaking down the problem into smaller, manageable steps and checking each step along the way to ensure accuracy. Here are some potential solutions to the issues mentioned:

1. For the table of values, you can use the "fprintf" command to print the values in a specific format. For example, you can use the following code to print the values in a table with four columns:

fprintf('%10.5f %10.5f %10.5f %10.5f\n', t, Paprox, Pexacta, error);

This will print each value in a column with a width of 10 and 5 decimal places. You can adjust the format as needed.

2. It may be helpful to use a "for" loop to print each row of the table separately, rather than trying to print the entire table at once. This way, you can check each row as it is printed and make adjustments if needed.

3. Make sure to check the dimensions of your vectors "t" and "P" before printing the table. They should be the same length, and if not, you may need to adjust your code.

4. If the values are still appearing spread out horizontally, you can try transposing the vectors before printing the table. For example:

fprintf('%10.5f %10.5f %10.5f %10.5f\n', t', Paprox', Pexacta', error');

5. It may also be helpful to plot the values first to ensure they are correct before printing the table.

I hope this helps! Remember to always check your code and break down the problem into smaller steps to troubleshoot any issues. Good luck with your differential equations course!
 

1. What is Euler's Method and how is it used in Matlab?

Euler's Method is a numerical method used to approximate solutions to differential equations. In Matlab, it is used to solve initial value problems by breaking the problem into smaller steps and using linear approximations to estimate the solution at each step.

2. How do I implement Euler's Method in Matlab?

To implement Euler's Method in Matlab, you first need to define the differential equation and initial conditions. Then, use a for loop to iterate through each step, calculating the new approximation using the previous approximation and the derivative function. Finally, plot the results to visualize the solution.

3. What are the advantages of using Euler's Method in Matlab?

Euler's Method is a simple and easy-to-understand numerical method that can be easily implemented in Matlab. It is also computationally efficient compared to other numerical methods. Additionally, it can provide a good approximation for simple differential equations.

4. What are the limitations of using Euler's Method in Matlab?

Euler's Method can produce significant errors if the step size is too large, and it may not provide an accurate solution for complex or non-linear differential equations. It also requires a lot of computational time for problems with small step sizes.

5. Are there any alternatives to using Euler's Method in Matlab?

Yes, there are many other numerical methods for solving differential equations in Matlab, such as the Runge-Kutta method, Adams-Bashforth method, and the trapezoidal method. Each method has its own advantages and limitations, so it is important to choose the most suitable method for the specific problem at hand.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
941
  • Engineering and Comp Sci Homework Help
Replies
3
Views
811
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
882
  • Engineering and Comp Sci Homework Help
Replies
6
Views
860
  • Engineering and Comp Sci Homework Help
Replies
1
Views
955
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top