Examen
Examen
Examen
function h=heun(f,a,b,ya,m)
% input - f is the function entered as a string 'f'
%- a and b are the left and right and points
%- ya is the initial condition y(a)
%- m is the number of stepe
%output - h=[t' y'] where t is the vector of abscisaas and
% y is the vector of ordinates
h=(b-a)/m;
t=zeros(1,m+1);
y=zeros(1,m+1);
t=a:h:b;
y(1)=ya;
for j=1:m
k1=feval(f,t(j),y(j));
k2=feval(f,t(j+1),y(j)+h*k1);
y(j+1)=y(j)+(h/2)*(k1+k2);
end
h=[t' y'];
Ejercicio 1
LET HWO =0.2 AND TO TWO STEPS BY HAD CALCULATION.THEN LET H=0.1
AND DO FOUR STEPS BY HAND CALCULATION.
function r=fz(t,y)
r=t.^2-y;
return;
>>> heun(@fz,0,0.4,1,2)
ans =
0.00000 1.00000
0.20000 0.82400
0.40000 0.69488
>>> heun(@fz,0,0.4,1,4)
ans =
0.00000 1.00000
0.10000 0.90550
0.20000 0.82193
0.30000 0.75014
0.40000 0.69093
Ejercicio 2
function h=heun(f,a,b,ya,m)
% input - f is the function entered as a string 'f'
%- a and b are the left and right and points
%- ya is the initial condition y(a)
%- m is the number of stepe
%output - h=[t' y'] where t is the vector of abscisaas and
% y is the vector of ordinates
h=(b-a)/m;
t=zeros(1,m+1);
y=zeros(1,m+1);
t=a:h:b;
y(1)=ya;
for j=1:m
k1=feval(f,t(j),y(j));
k2=feval(f,t(j+1),y(j)+h*k1);
y(j+1)=y(j)+(h/2)*(k1+k2);
end
h=[t' y'];
function r = fz1(t,y)
r = 3*y + 3*t;
return;
Ojo----------y( 0 )= 1
>>> heun(@fz1,0,0.4,1,2)
ans =
0.00000 1.00000
0.20000 1.84000
0.40000 3.49120
>>> heun(@fz1,0,0.4,1,4)
ans =
0.00000 1.00000
0.10000 1.36000
0.20000 1.87870
0.30000 2.61085
0.40000 3.63010
function r=rk4(f,a,b,ya,m)
%input - f is the function entered as a string 'f'
% - a and b are the left and right end points
% - ya is the initial condition y(a)
% - m is the number of steps
%output -r=[t' y'] where t is the vector of abscissas
% and y is the vector of ordinates
h=(b-a)/m;
t=zeros(1,m+1);
y=zeros(1,m+1);
t=a:h:b;
y(1)=ya;
for j=1:m
k1=h*feval(f,t(j),y(j));
k2=h*feval(f,t(j)+h/2,y(j)+k1/2);
k3=h*feval(f,t(j)+h/2,y(j)+k2/2);
k4=h*feval(f,t(j)+h,y(j)+k3);
y(j+1)=y(j)+(k1+2*k2+2*k3+k4)/6;
end
r=[t' y'];
Ejercicio 1
function r=rk4(f,a,b,ya,m)
%input - f is the function entered as a string 'f'
% - a and b are the left and right end points
% - ya is the initial condition y(a)
% - m is the number of steps
%output -r=[t' y'] where t is the vector of abscissas
% and y is the vector of ordinates
h=(b-a)/m;
t=zeros(1,m+1);
y=zeros(1,m+1);
t=a:h:b;
y(1)=ya;
for j=1:m
k1=h*feval(f,t(j),y(j));
k2=h*feval(f,t(j)+h/2,y(j)+k1/2);
k3=h*feval(f,t(j)+h/2,y(j)+k2/2);
k4=h*feval(f,t(j)+h,y(j)+k3);
y(j+1)=y(j)+(k1+2*k2+2*k3+k4)/6;
end
r=[t' y'];
Ejercicio 1
function y=F1(t,y)
y=3.*y+3.*t;
return;
euler(@F2,0,80,1,10)
ans =
0.0000e+000 1.0000e+000
8.0000e+000 2.5000e+001
1.6000e+001 8.1700e+002
2.4000e+001 2.0809e+004
3.2000e+001 5.2080e+005
4.0000e+001 1.3021e+007
4.8000e+001 3.2552e+008
5.6000e+001 8.1380e+009
6.4000e+001 2.0345e+011
7.2000e+001 5.0863e+012
8.0000e+001 1.2716e+014
COMANDOS
function s=traprl(f,a,b,M)
%input - f is the integrand input as string 'f'
% - a and b are upper and lower limits of integration
% -M is the number of subintervals
%Output - s is the trapezoidal rule sum
h=(b-a)./M;
s=0;
for k=1:(M-1);
x=a+h.*k;
s=s+feval(f,x);
end
s=h.*(feval(f,a)+feval(f,b))./2+h.*s;
function s=simprl(f,a,b,M)
%Input - f is the integrand input as a string 'f'
% - a and b are upper and lower limits of integration
% - M is the number of subintrevals
%Output - s is the simsonp rule sum
h=(b-a)./(2.*M);
s1=0;
s2=0;
for k=1:M;
x=a+h.*(2.*k-1);
s1=s1+feval(f,x);
end
for k=1:(M-1);
x=a+h.*2.*k;
s2=s2+feval(f,x);
end
s=h.*(feval(f,a).*feval(f,b)+4.*s1+2.*s2)./3
RESOLUCION DE EJERCICIOS PROPUESTOS: REGLA
TRAPEZOIDAL Y SIMPSON
a) function y =F(x);
y=1./(1+x.^ 2);
return;
simpr1(@F1, -1, 1, 5)
s = 1.5208
ans = 1.5208
b) function y =F(x);
y=2+sin(2.*sqrt(x));
return;
trapr1(@F1, 0, 1, 10)
ans = 2.8574
simpr1(@F1, 0, 1, 5)
s = 2.8959
ans = 2.8959
a) function y=F1(x)
y=2.*pi.*x.^3.*sqrt(1+9.*x.^4);
return;
trapr1(@F1, 0, 1, 10)
ans = 3.6424
simpr1(@F1, 0, 1, 5)
s = 2.9014
ans = 2.9014
b) function y=F1(x)
y=2.*pi.*sin(x).*sqrt(1+(cos(x))^2);
return;
simpr1(@F1, 0, pi/4, 5)
s = 2.2800
ans = 2.2800