code of euler's method
96 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Joaquim
el 22 de Mayo de 2014
Respondida: Sandip Das
el 28 de Jul. de 2021
Hi, i follow every protocol steps for euler's method, but my results are too increased and they are not correct. Anyone could see if i´m doing anything wrong? i think it happens because my derivatives are floating too much.
1 comentario
Sara
el 22 de Mayo de 2014
What's the expected result? What are the functions you're trying to solve?
Respuesta aceptada
George Papazafeiropoulos
el 23 de Mayo de 2014
A simple application of Euler method:
Define the function:
function E=euler(f,a,b,ya,M)
h=(b-a)/M;
Y=zeros(1,M+1);
T=a:h:b;
Y(1)=ya;
for j=1:M
Y(j+1)=Y(j)+h*f(T(j));
end
E=[T' Y'];
end
where - f is the function entered as function handle
- a and b are the left and right endpoints
- ya is the initial condition E(a)
- M is the number of steps
- E=[T' Y'] where T is the vector of abscissas and Y is the vector of ordinates
Then run the code:
f=@(x) x^2;
a=0;
b=10;
ya=0;
M=200;
YY=euler(f,a,b,ya,M)
You can adjust your problem according to the above algorithm.
2 comentarios
Rachel Lee
el 6 de Ag. de 2020
How would you find the error between Euler's Method and the Exact Soln using truncation? I think we need the derivative but nothing I do seems to work.
Rachel Lee
el 6 de Ag. de 2020
%------------------------------------Functions
function [E] = odeEuler(f,a,b,ya,M)
%M is the no of steps taken
h=(b-a)/M;
Y=zeros(1,M+1);
T=a:h:b;
Y(1)=ya; %this value is 4 for this problem
for j=1:M
Y(j+1)=Y(j)+h*f(T(j));
end
E=[T' Y'];
end
%------------------------------------Executable
%goal print out three iterations of this soln
y0 = 4; %initial y value
t = [0 2 4]'; %this is our specific system of t
size = length(t);
fn = @(t)(4/1.3)*(exp(0.8*t) - exp(-0.5*t))+2*exp(-0.5*t);
dfn = @(t) 4*exp(0.8*t) - 0.5 * fn;
h = 2; err = 0; %initial conditions
a = t(1,:);%0
b = t(size,:);%4
[Soln] = odeEuler(fn,a,b,y0,h);
A = t;
B = Soln(:,2);
C = fn(t);
%producing the graph
plot(t,B,t,C);
title('Comparing Linearization Methods')
legend('Eulers Method','Exact Soln: 4/1.3)*(exp(0.8*t) - exp(-0.5*t))+2*exp(-0.5*t)')
%producing a Table with M iterations
Data = [A B C];
VarNames = {'time domain','Eulers Method','Exact Soln'};
T = table(Data(:,1),Data(:,2),Data(:,3),'VariableNames',VarNames)
Más respuestas (3)
SkyRazor
el 23 de Mayo de 2014
hello, could you please post your equation and give us some explanations?
0 comentarios
ahmed abdelmageed
el 4 de Mayo de 2020
function E=euler(f,a,b,ya,M)
h=(b-a)/M;
Y=zeros(1,M+1);
T=a:h:b;
Y(1)=ya;
for j=1:M
Y(j+1)=Y(j)+h*f(T(j));
end
E=[T' Y'];
end
0 comentarios
Sandip Das
el 28 de Jul. de 2021
%Published in 19th july 2021
%Sandip Das
clc
clear all
dydt=input('\n Enter the function : ');
x0=input('\n Enter initial value of x : ');
y0=input('\n Enter initial value of y : ');
xn=input('\n Enter the final value of x: ');
h=input('\n Enter the step length h: ');
i=0;
while i<xn
tempy=y0+h*dydt(x0,y0);
tempx=x0+h;
x0=tempx;
y0=tempy;
i=i+h;
end
fprintf('The value of y at t=%f is %f \n',x0,y0);
0 comentarios
Ver también
Categorías
Más información sobre Biological and Health Sciences en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!