Register to reply

Help with Euler's Method in Matlab

by Deathcrush
Tags: euler's method, matlab, table
Share this thread:
Deathcrush
#1
Nov6-10, 03:18 PM
P: 40
1. The problem statement, all variables and given/known data
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?


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


3. 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
Phys.Org News Partner Science news on Phys.org
Scientists develop 'electronic nose' for rapid detection of C. diff infection
Why plants in the office make us more productive
Tesla Motors dealing as states play factory poker
viscousflow
#2
Nov6-10, 04:16 PM
P: 273
The issue is you're printing arrays. You need to write your print statement in a loop, looping your values.
JesseC
#3
Nov6-10, 04:26 PM
P: 282
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);

Deathcrush
#4
Nov6-10, 04:56 PM
P: 40
Help with Euler's Method in Matlab

It worked!, thanks a lot


Register to reply

Related Discussions
Euler's method/matlab Engineering, Comp Sci, & Technology Homework 2
Matlab Help Euler method Engineering, Comp Sci, & Technology Homework 10
Euler methond and the improved Euler method Differential Equations 4
Euler Method in MATLAB Mechanical Engineering 0
Euler method Differential Equations 12