Help with Euler's Method in Matlab

AI Thread Summary
The discussion focuses on a user's struggle with implementing Euler's Method in MATLAB for approximating solutions to differential equations. The user is having trouble formatting the output table correctly, as the values are displayed horizontally instead of in columns. A solution is provided, suggesting the use of a loop to print the arrays properly, along with a newline command in the print statement. The user successfully implements this advice, resulting in the desired output format. This exchange highlights common issues faced by beginners in MATLAB programming and effective troubleshooting strategies.
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
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
2
Views
3K
Replies
1
Views
1K
Replies
3
Views
1K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
6
Views
2K
Back
Top