Computational Homework #3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Computational Homework #3

1.

function​ [ p ] = fixed_point(g, pO, TOL, NO)

i = 1;

while​ i<=NO

p = g(pO);

​if​ abs(p-pO)<TOL
​return
​end

i = i+1;
pO = p;

end

fprintf(​'Error: reached max iterations with NO=%d'​,NO)

end

%y = x^3 - 25
%g = @(x) x - (x.^3 - 25)/(3*x.^2)
%fixed_point(g, 2.5, 10^-10, 20)

g1 = @(x) (((20.*x) + 21/(x.^2))/21)


fixed_point(g1,1,10^-4,200)

g2 = @(x) (x - ((x.^3) - 21)/(3.*(x.^2)))


fixed_point(g2,1,10^-4,200)

g3 = @(x) (x + (((x.^3) - 21)/5))


fixed_point(g3,1,10^-4,200)

g4 = @(x) ((21/x).^0.5)
fixed_point(g4,1,10^-4,200)
2.

function​ [ root ] = newton(f_name,fd_name,x,x_tol,f_tol,n_max)

% f_name is the function file


% fd_name is the function file with the derivative of the original function
% x is the initial starting point
% x_tol and f_tol are the tolerances that will determine the termination
% n_max is the maximum number of iteration

n = 0;
fx = f_eval(f_name,x);

disp(​'|n| |x| |f(x)|'​)

disp(​'~~~~~~~~~~~~~~~~~~~~~~~~~~'​)

fprintf(​'%4d %23.15e %23.15e'​, n, x, fx)

if​ abs(fx) <= xtol


root = x;

return
end

for​ n = 1:n_max
fdx = f_eval(fd_name,x);
d = fx/fdx;
x = x - d;
fx = f_eval(f_name,x);

fprintf(​'%4d %23.15e %23.15e'​, n, x, fx)

if​ abs(d) <= xtol || abs(fx) <= ftol


root = x;
return
end

end

Output
>> newton(@f_a,@f_a_d,0,.01,.01,15)
|n| |x| |f(x)|
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 0.000000000000000e+00 1.000000000000000e+00 1 -1.000000000000000e+00
6.000000000000000e+00 2 -6.666666666666667e-01 2.012345679012346e+00 3
-3.729729729729731e-01 9.057974814330254e-01 4 3.269342512929116e-01
1.163635389737034e+00 5 2.837413335432363e+00 -4.556422332425985e+01 6
1.232396624208102e+00 -4.819674397136765e+00 7 8.952375908891554e-01
-1.049882131517538e+00 8 7.664461621095491e-01 -1.396730060190052e-01 9
7.432085053559998e-01 -4.280256808372851e-03
ans =

0.7432

>> newton(@f_a,@f_a_d,4.5,.01,.01,15)
|n| |x| |f(x)|
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 4.500000000000000e+00 -4.006250000000000e+01 1 5.148785425101215e+00
2.645714149184744e+01 2 4.971609740327156e+00 2.482935203036507e+00 3
4.951220650781798e+00 3.052352645146694e-02 4 4.950963696479096e+00
4.807674133466833e-06
ans =

4.9510

>> newton(@f_b,@f_b_d,1,.1,.1,10)
|n| |x| |f(x)|
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 1.000000000000000e+00 1.087659395245250e-01 1 9.149131083388199e-01
1.100522853399757e-01
ans =

0.9149
>> newton(@f_b,@f_b_d,-4,.1,.1,10)
|n| |x| |f(x)|
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 -4.000000000000000e+00 2.142203362609847e-01 1 -3.838333165876600e+00
2.098660879287455e-01 2 -3.663692695414265e+00 2.052327405122272e-01 3
-3.425678708780418e+00 1.990358025332347e-01 4 -1.713782480521354e+00
1.584843267255913e-01 5 -1.938048448454631e+00 1.633937423018033e-01 6
-2.034191214407477e+00 1.655357051039649e-01
ans =

-2.0342

>> newton(@f_c,@f_c_d,0,-.5,1,4)
|n| |x| |f(x)|
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 0.000000000000000e+00 9.911504424778761e-01 1 -7.031111111111111e+00
-1.000000000000000e+00
ans =

-7.0311

***Another output that I was getting for this function was NaN

You might also like