You are on page 1of 9

Miguel ngel Ortiz Chacn

A01018615
Numerical Methods
Dr. Lino Notarantonio

February 2nd 2012


Hwk. 1st Partial

Homework1
1.- Evaluate e^-5 by using the McLaurin expansion
Source Code in Matlab
clc;
x=5;
acum=1;
tv=0.006734947;
fprintf('Problem 1\n');
fprintf('\nMcLaurin\n True value of e^-5=0.006734947\n');
fprintf('Terms || Answers
|| Et porcentage ||
Ea
porcentage\n');
for a=1:21
acumaf=acum;
if rem(a,2)==0
acum=acum+(x^a/feval(@factorial,a));
else
acum=acum-(x^a/feval(@factorial,a));
end
Et=((tv-acum)/tv)*100;
Ea=((acum-acumaf)/acum)*100;
fprintf('%1.0f
|| %.9f
||%f
||%f\n',a,acum,Et,Ea);
end
fprintf('Mc Laurin method of e^5 at 20 terms= %.9f\n\n',acum);
fprintf('Now using 1/e^x at 20 terms\n True value of e^5=0.006734947\n');
fprintf('Terms || Answers
|| Et porcentage ||
Ea
porcentage\n');
acum=1;
answ=1;
for a=1:21
acumaf=answ;
acum=acum+(x^a/feval(@factorial,a));
answ=1/acum;
Et=((tv-answ)/tv)*100;
Ea=((answ-acumaf)/answ)*100;
fprintf('%1.0f
|| %.9f
||%f
||%f\n',a,answ,Et,Ea);
end
fprintf('The Answer is: %.9f\n',answ);

Program Output
Problem 1
McLaurin
True value of e^-5=0.006734947
N || Answers || Et porcentage || Ea porcentage
1||-4.000000000||59491.707166||125.000000
2||8.500000000||-126107.377727||147.058824

3||-12.333333333||183224.430427||168.918919
4||13.708333333||-203440.329766||189.969605
5||-12.333333333||183224.430427||211.148649
6||9.368055556||-138996.203067||231.653076
7||-6.132936508||91161.392286||252.749919
8||3.555183532||-52687.104809||272.506889
9||-1.827105379||27228.726910||294.580103
10||0.864039076||-12729.188949||311.460966
11||-0.359208403||5433.500078||340.539772
12||0.150478046||-2134.287017||338.711501
13||-0.045555204||776.400327||430.320215
14||0.024456671||-263.130867||286.269025
15||0.001119380||83.379531||-2084.841268
16||0.008412283||-24.904968||86.693508
17||0.006267312||6.943414||-34.224748
18||0.006863137||-1.903359||8.681532
19||0.006706341||0.424739||-2.338029
20||0.006745540||-0.157286||0.581111
21||0.006736207||-0.018708||-0.138551
Mc Laurin method of e^5 at 20 terms= 0.006736207
Now using 1/e^x at 20 terms
True value of e^-5=0.006734947
Terms || Answers || Et porcentage || Ea porcentage
1 || 0.166666667 ||-2374.654465 ||-500.000000
2 || 0.054054054 ||-702.590637 ||-208.333333
3 || 0.025423729 ||-277.489664 ||-112.612613
4 || 0.015296367 ||-127.119339 ||-66.207627
5 || 0.010938924 ||-62.420348 ||-39.834289
6 || 0.008840322 ||-31.260449 ||-23.738985
7 || 0.007774898 ||-15.441119 ||-13.703376
8 || 0.007230283 ||-7.354717 ||-7.532415
9 || 0.006959453 ||-3.333447 ||-3.891547
10 || 0.006831506 ||-1.433706 ||-1.872889
11 || 0.006774891 ||-0.593087 ||-0.835662
12 || 0.006751577 ||-0.246927 ||-0.345307
13 || 0.006742653 ||-0.114423 ||-0.132353
14 || 0.006739472 ||-0.067184 ||-0.047207
15 || 0.006738412 ||-0.051448 ||-0.015728
16 || 0.006738081 ||-0.046532 ||-0.004914
17 || 0.006737983 ||-0.045086 ||-0.001445
18 || 0.006737956 ||-0.044684 ||-0.000401
19 || 0.006737949 ||-0.044578 ||-0.000106
20 || 0.006737948 ||-0.044552 ||-0.000026
21 || 0.006737947 ||-0.044546 ||-0.000006
The Answer is: 0.006737947

2.-

( )

f(0.577)

3 digit
(

Applying cut
(

4 digit
(

Applying cut
(

Answer: In both cases we get a division by Zero.


3.x=1.37
y=0.749
True value y=0.743053

a)

[(

x=1.37
[(

4.-

True roots

x1=5000, x2=2x10-3

Alternative formula

5 digit

x1=4.1305x10-3
x2=5137.98

)
( )(

Homework 2
Bisection Method
1.( )
( )
Source Code in Matlab
%Problem 1
%F(x)=-2x^6-1.5x^4 + 10x + 2, 0 <=_ x <=1
%Find all critical points%
%F'(x)=-12x^5-1.5x^3+10=0, a=0,b=1
%
clc;
a=0; %a and b are the intervals
b=1;
fl=1; %fl is the condition to continue finding the roots
n=0; %counter for the number of iteratios
p=(a+b)/2; %p is the midpoint
fx='-2*x^6-1.5*x^4 + 10*x + 2'; %the main ecuation
fpx='(-12*x^5)-(6*x^3)+10'; %the derivative of the ecuation for
finding the root
%
fprintf('N ||P
||a
||b
||Ea Porcentage
\n');
while fl==1;
aux=p;
ev=(subs(fpx,a))*(subs(fpx,p));
if ev<0 %if f(a)f(p) < 0
b=p; %set b as base point
p=(a+b)/2;
elseif ev>0 %if f(a)f(p) < 0
a=p; %set a as base point
p=(a+b)/2;
else
fl=0; %Break the condition set fl=0
end

n=n+1;
Ea=abs((p-aux)/p*100); %Get the relative Error
fprintf('%1.0f ||%f||%f||%f||%f,ev %f\n',n,p,a,b,Ea,ev);
if Ea<0.001 %if the relative error is less than the tolerance
fl=0; %break the condition
end
end
fprintf('The Value of root is: %f\n',p);
y=subs(fx,p);%get y
fprintf('The critical points are in: (%f,%f)\n',p,y);

Output of the program


N ||P
||a
||b
||Ea %
f(a)f(p)
1 ||0.750000||0.500000||1.000000||33.333333,ev 88.750000
2 ||0.875000||0.750000||1.000000||14.285714,ev 41.012207
3 ||0.812500||0.750000||0.875000||7.692308,ev -0.806097
4 ||0.843750||0.812500||0.875000||3.703704,ev 11.703507
5 ||0.859375||0.843750||0.875000||1.818182,ev 3.202168
6 ||0.867188||0.859375||0.875000||0.900901,ev 0.717322
7 ||0.871094||0.867188||0.875000||0.448430,ev 0.114704
8 ||0.873047||0.871094||0.875000||0.223714,ev 0.003097
9 ||0.872070||0.871094||0.873047||0.111982,ev -0.001213
10 ||0.871582||0.871094||0.872070||0.056022,ev -0.000488
11 ||0.871338||0.871094||0.871582||0.028019,ev -0.000126
12 ||0.871460||0.871338||0.871582||0.014008,ev 0.000054
13 ||0.871399||0.871338||0.871460||0.007004,ev -0.000008
14 ||0.871429||0.871399||0.871460||0.003502,ev 0.000002
15 ||0.871414||0.871399||0.871429||0.001751,ev -0.000001
16 ||0.871407||0.871399||0.871414||0.000876,ev -0.000000
The Value of root is: 0.871407
The critical points are in: (0.871407,8.973449)
2.-

( )

( )

Source Code in MatLab


%Problem 2
%v(m)=35-((gm)/c)(1-e^((-c/m)*t)))=0
%Find the point where the m where v(m)=35
%
clc;
a=0; %a and b are the intervals
b=100;
fl=1; %fl is the condition to continue finding the roots
n=0; %counter for the number of iteratios
p=(a+b)/2; %p is the midpoint
fx='35-((9.81*x)/15)*(1-exp((-15*9)/x))'; %main function
%35-((9.81*x)/15)(1-exp((-15/x)*9))
fprintf('N ||P
||a
||b
||Ea Porcentage
while fl==1;
aux=p;
ev=(subs(fx,a))*(subs(fx,p));
if ev<0 %if f(a)f(b) < 0
b=p; %set b as base point
p=(a+b)/2;
elseif ev>0 %if f(a)f(b) < 0
a=p; %set a as base point
p=(a+b)/2;

\n');

else
fl=0; %Break the condition set fl=0
end
Ea=abs((p-aux)/p*100); %Get the relative Error
if Ea<0.001 %if the relative error is less than the tolerance
fl=0; %break the condition
end
n=n+1;
fprintf('%1.0f ||%f||%f||%f||%f,ev %f\n',n,p,a,b,Ea,ev);
end
fprintf('The Value of mass is: %f\n',p);
y=subs(fx,p)

Program output
N ||P
||a
||b
||Ea Porcentage, f(a)f(p)
1 ||75.000000||50.000000||100.000000||33.333333,ev 157.416709
2 ||62.500000||50.000000||75.000000||20.000000,ev -26.725262
3 ||56.250000||50.000000||62.500000||11.111111,ev -5.222122
4 ||59.375000||56.250000||62.500000||5.263158,ev 6.970352
5 ||60.937500||59.375000||62.500000||2.564103,ev 0.256859
6 ||60.156250||59.375000||60.937500||1.298701,ev -0.083646
7 ||59.765625||59.375000||60.156250||0.653595,ev -0.028382
8 ||59.570312||59.375000||59.765625||0.327869,ev -0.000530
9 ||59.667969||59.570312||59.765625||0.163666,ev 0.013451
10 ||59.716797||59.667969||59.765625||0.081766,ev 0.003161
11 ||59.741211||59.716797||59.765625||0.040866,ev 0.000696
12 ||59.753418||59.741211||59.765625||0.020429,ev 0.000131
13 ||59.759521||59.753418||59.765625||0.010213,ev 0.000015
14 ||59.756470||59.753418||59.759521||0.005107,ev -0.000001
15 ||59.757996||59.756470||59.759521||0.002553,ev 0.000002
16 ||59.758759||59.757996||59.759521||0.001277,ev 0.000000
17 ||59.758377||59.757996||59.758759||0.000638,ev -0.000000
The Value of mass is: 59.758377
3.( )
a=2
b=3
Interval (2,3)
N
1
2
3
Root=2.625

P
2.5
2.75
2.625

a
2
2.5
2.5

b
3
3
2.75

F(a)F(p)
>0
<0
>0

Fixed-Point Method
1)
( )
N
0
1
2
3
4
5

Xn
5
3.4
3.0235294
3.000091554
3
3

Xn+1
3.4
3.0235294
3.000091554
3
3
3

Et
13.33%
0.78%
3.0518x10-3%
3.33x10-8%
0%
0%

b) We reach the true value at the 4th iteration, so if we continue the True
Error will remain the same.
2)
( )
( )

( )
( )

Ea<0.001%
Source Code in MatLab
%f(x)=2sin(x^1/2)-x
%set g(x)=2sin(x^1/2)
clc;
ea=100; %set de ea at 100%
xn=.5;%set base point
n=0; %number of iteratios
fprintf('N ||Xn
||Xn+1
||Ea\n');
while ea>=0.001; %as long as de error is more than 0.001%
xo=xn;
%save the old value
xnq=2*sin(xn^(1/2)); %evaluate xn+1=g(x)
xn=xnq; %xn=xn+1
ea=abs(xn-xo)/xn*100;%compute relative error
n=n+1; %increase number of iteration
fprintf('%1.0f ||%f||%f||%f\n',n,xn,xnq,ea);
end
fprintf('The aproximate root is:%f',xn);

Output of the Program


N ||Xn ||Xn+1 ||Ea
1 ||1.299274||1.299274||61.516967
2 ||1.817148||1.817148||28.499262

3 ||1.950574||1.950574||6.840367
4 ||1.969743||1.969743||0.973152
5 ||1.972069||1.972069||0.117966
6 ||1.972344||1.972344||0.013958
7 ||1.972377||1.972377||0.001647
8 ||1.972380||1.972380||0.000194
The approximate root is:1.972380
3)
( )

( )
f(3)<0
f(4)>0

Interval (3,4)
g(3)<1

n
0
1
2
3

xn
3
3.11764706
3.569272961
3.712447042

Xn+1
3.11764706
3.569272961
3.712447042
3.760037309

Ea

1.26%

You might also like