Help with Euler's Method in Matlab

Click For Summary

Discussion Overview

The discussion revolves around a homework problem related to implementing Euler's Method in Matlab for approximating solutions to differential equations. Participants focus on issues with displaying results in a formatted table and calculating errors, as well as providing assistance with coding in Matlab.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • The original poster describes difficulties in formatting output in Matlab, specifically that values are displayed horizontally instead of in columns.
  • Some participants suggest using a loop to print the arrays correctly, indicating that the print statement needs to be adjusted.
  • One participant recommends adding a newline command to the print statement to improve formatting.
  • The original poster confirms that the suggested changes resolved their issue.

Areas of Agreement / Disagreement

Participants generally agree on the need for a loop to format the output correctly, and the original poster acknowledges that the provided solution worked for them. No significant disagreements are noted.

Contextual Notes

The discussion is limited to the specific issue of output formatting in Matlab and does not address other potential errors or improvements in the script.

Deathcrush
Messages
36
Reaction score
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
The issue is you're printing arrays. You need to write your print statement in a loop, looping your values.
 
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);
 
It worked!, thanks a lot
 

Similar threads

Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
3K
Replies
2
Views
2K