0% found this document useful (0 votes)
16 views5 pages

U X 'Ingresar F (U, X) : ' 'Ingresar A: ' 'Ingresar U (A) : ' 'Ingresar L: ' 'Ingresar N: '

This document contains MATLAB code implementing several numerical methods for approximating solutions to differential equations: Euler's method, Simpson's rule (simple and composite), modified Euler's method, backward Euler's method, and Runge-Kutta method. For each method, the user inputs the differential equation f(u,x), initial value a, boundary value u(a), endpoint L, and number of intervals n, and the code returns an approximated solution f(L).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

U X 'Ingresar F (U, X) : ' 'Ingresar A: ' 'Ingresar U (A) : ' 'Ingresar L: ' 'Ingresar N: '

This document contains MATLAB code implementing several numerical methods for approximating solutions to differential equations: Euler's method, Simpson's rule (simple and composite), modified Euler's method, backward Euler's method, and Runge-Kutta method. For each method, the user inputs the differential equation f(u,x), initial value a, boundary value u(a), endpoint L, and number of intervals n, and the code returns an approximated solution f(L).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

%Euler

clear,clc
syms u x
f=input('ingresar f(u,x): ');
a=input('ingresar a: ');
ua=input('ingresar u(a): ');
L=input('ingresar L: ');
n=input('ingresar n: ');
h=(L-a)/n;
s=ua+subs(f,[u,x],[ua,a])*h;
for c=1:(n-1);
s=s+subs(f,[u,x],[s,a+c*h])*h;
end
fprintf('f(L) aproximada es:')
double(s)

ingresar f(u,x): 10*u+cos(x)


ingresar a: 0
ingresar u(a): 10
ingresar L: 5
ingresar n: 50
f(L) aproximada es:
ans =

1.1370e+16

>>
%simpson simple
function [I]=simpson(f,a,b)
I=(b-a)/6*(f(a)+4*f((a+b)/2)+f(b));
end

%Simpson compuesta
function [I]=simpsonc(f,a,b,n)
h=(b-a)/n;
I=0;
for c=1:n
I=I+simpson(f,a,a+h);
a=a+h;
end;
end

%regla de simpson compuesta


f=input('Ingresar f(x)= ','s');
a=input('Ingresar a= ');
b=input('Ingresar b= ');
n=input('Ingresar n= ');
f=inline(f);
simpsonc(f,a,b,n)
%Euler-modificado
clear,clc
syms u x
f=input('ingresar f(u,x): ');
a=input('ingresar a: ');
ua=input('ingresar u(a): ');
L=input('ingresar L: ');
n=input('ingresar n: ');
h=(L-a)/n;
ue=ua+subs(f,[u,x],[ua,a])*h;
s=ua+(subs(f,[u,x],[ua,a])+subs(f,[u,x],[ue,a+h]))*h/2;
for c=1:(n-1);
ue=s+subs(f,[u,x],[s,a+c*h])*h;

s=s+(subs(f,[u,x],[s,a+c*h])+subs(f,[u,x],[ue,a+(c+1)*h]))*h/
2;
end
fprintf('f(L) aproximada es:')
double(s)

ingresar f(u,x): 10*u+cos(x)


ingresar a: 0
ingresar u(a): 10
ingresar L: 5
ingresar n: 50
f(L) aproximada es:
ans =

7.9666e+20

>>
%Euler-retrazado
clear,clc
syms u x
f=input('ingresar f(u,x): ');
a=input('ingresar a: ');
ua=input('ingresar u(a): ');
L=input('ingresar L: ');
n=input('ingresar n: ');
h=(L-a)/n;
ue=ua+subs(f,[u,x],[ua,a])*h;
s=ua+subs(f,[u,x],[ue,a+h])*h;
for c=1:(n-1);
ue=s+subs(f,[u,x],[s,a+c*n])*h;
s=s+subs(f,[u,x],[s,a+(c+1)*h])*h;
end
fprintf('f(L) aproximada es:')
double(s)

ingresar f(u,x): 10*u+cos(x)


ingresar a: 0
ingresar u(a): 10
ingresar L: 5
ingresar n: 50
f(L) aproximada es:
ans =

1.7054e+16

>>
%Runge-kutta
clear,clc
syms u x
f=input('ingresar f(u,x): ');
a=input('ingresar a: ');
ua=input('ingresar u(a): ');
L=input('ingresar L: ');
n=input('ingresar n: ');
h=(L-a)/n;
ue1=ua+subs(f,[u,a],[ua,a])*h/2;
ue2=ua+subs(f,[u,a],[ua,a])*h;
s=ua+(subs(f,[u,x],[ua,a])+4*subs([u,x],[ue1,a+h/2])+subs(f,[
u,x],[ue2,a+h]))*h/6;
for c=1:(n-1);
ue1=s+subs(f,[u,a],[s,a+c*h])*h/2;
ue2=s+subs(f,[u,a],[s,a+c*h])*h;

s=s+(subs(f,[u,x],[s,a+c*h])+4*subs([u,x],[ue1,a+h*(c+1/2)])+
subs(f,[u,x],[ue2,a+(c+1)*h]))*h/6;
end
fprintf('f(L) aproximada es:')
double(s)

You might also like