Вы находитесь на странице: 1из 10

Mehmed Duhovi

Numerical Analysis Homework


2.1.)
>>linspace(4,35,6)
ans=
4.000010.200016.400022.600028.800035.0000
>>linspace(4,2)
ans=
Columns1through13
4.00003.93943.87883.81823.75763.69703.63643.5758
3.51523.45453.39393.33333.2727
Columns14through26
3.21213.15153.09093.03032.96972.90912.84852.7879
2.72732.66672.60612.54552.4848
Columns27through39
2.42422.36362.30302.24242.18182.12122.06062.0000
1.93941.87881.81821.75761.6970
Columns40through52
1.63641.57581.51521.45451.39391.33331.27271.2121
1.15151.09091.03030.96970.9091
Columns53through65
0.84850.78790.72730.66670.60610.54550.48480.4242
0.36360.30300.24240.18180.1212
Columns66through78
0.060600.06060.12120.18180.24240.30300.3636
0.42420.48480.54550.60610.6667
Columns79through91
0.72730.78790.84850.90910.96971.03031.09091.1515
1.21211.27271.33331.39391.4545
Columns92through100
1.51521.57581.63641.69701.75761.81821.87881.9394
2.0000

2.2.)
>>v=2:0.5:1.5
v=
2.00001.50001.00000.500000.50001.00001.5000

Mehmed Duhovi
Numerical Analysis Homework
>>t=8:0.5:4.5
t=
8.00007.50007.00006.50006.00005.50005.00004.5000

2.3.)
>>linspace(3,5,6)
ans=
3.00001.40000.20001.80003.40005.0000

2.4.)
>>A=[321;0:0.5:1;linspace(6,8,3)]
A=
3.00002.00001.0000
00.50001.0000
6.00007.00008.0000
>>A(2,:)*A(:,3)
ans=
8.5000

2.8.)
>>F=[14188913]
F=
14188913
>>x=[0.0130.0200.0090.0100.012]
x=
0.01300.02000.00900.01000.0120
>>k=F./x
k=
1.0e+03*
1.07690.90000.88890.90001.0833
>>U=0.5*k.*x.^2
U=
0.09100.18000.03600.04500.0780
>>max(U)
ans=

Mehmed Duhovi
Numerical Analysis Homework
0.1800

Example 4.4.)
fx = @(x) -0.1*x^4 - 0.15*x^3 - 0.5*x^2 - 0.25*x + 1.2;
fxder = @(x) -0.4*x^3 - 0.45*x^2 - 1.0*x - 0.25;
truevalue = -0.9125;
truerror = @(t) abs(((truevalue - t) / truevalue)) * 100%;
disp('Function: ');
disp(fx);
disp('Derivative: ');
disp(fxder);
h = 0.5;
x = 0.5;
xplus = x + h;
xminus = x - h;
disp('Function of 0.5: ');
disp(fx(x));
disp('Function of 1.0: ');
disp(fx(xplus));
disp('Function of 0: ');
disp(fx(xminus));
disp('Computing Differences: ');
disp('Forward: ');
fxdiffForward = ((0.2 - 0.925) / 0.5);
disp(fxdiffForward);
disp('Error: ');
disp(truerror(fxdiffForward));
disp('Backward: ');
fxdiffForward = ((0.925 - 1.2) / 0.5);
disp(fxdiffForward);
disp('Error: ');
disp(truerror(fxdiffForward));
disp('Centered: ');
fxdiffForward = ((0.2 - 1.2) / 1.0);
disp(fxdiffForward);
disp('Error: ');
disp(truerror(fxdiffForward));

h = 0.25;
x = 0.5;
xplus = x + h;
xminus = x - h;
disp('Function of 0.5: ');
disp(fx(x));
disp('Function of 0.75: ');
disp(fx(xplus));
disp('Function of 0.25: ');
disp(fx(xminus));
disp('Computing Differences: ');
disp('Forward: ');

Mehmed Duhovi
Numerical Analysis Homework
fxdiffForward = ((0.6363 - 0.925) / 0.25);
disp(fxdiffForward);
disp('Error: ');
disp(truerror(fxdiffForward));
disp('Backward: ');
fxdiffForward = ((0.925 - 1.1035) / 0.25);
disp(fxdiffForward);
disp('Error: ');
disp(truerror(fxdiffForward));
disp('Centered: ');
fxdiffForward = ((0.6363 - 1.1035) / 0.5);
disp(fxdiffForward);
disp('Error: ');
disp(truerror(fxdiffForward));

Example 5.5)
a = 50;
b = 200;
cd = 0.25; g = 9.81; v = 36; t = 4;
%%f = @(x) x.^3 - 2*x.^2 - 5;
f = @(x) sqrt(g*x/cd).*tanh(sqrt(g*cd./x)*t)-v;
toll = 1e-5;
iter = 0;
x = b - (f(b) * (a - b)) / (f(a) - f(b));
disp('Error is: ');
root = 142.7376;
step = ((x - root) / x) * 100;
disp('False Position: ');
while (step >= toll)
x = b - (f(b) * (b - a)) / (f(b) - f(a));
if abs(f(x)) <= toll
disp('iterations:');
disp(iter);
disp('x is');
disp(x);
break;
elseif f(a) * f(x) < 0
step = b - x;
b = x;
elseif f(x) * f(a) < 0
step = x - a;
a = x;
end
iter = iter + 1;
end

5.5.)
>>x=[1:0.1:10];
>>f=1221*x+18*x.^22.75*x.^3;
>>plot(f,x)
>>grid

Mehmed Duhovi
Numerical Analysis Homework

a = -1;
b= 0;
fx1 = @(x)-12-21*x+18*x.^2-2.75*x.^3;
toll = 0.101;
xr = (a + b) / 2;
disp('Checking for the first root: ');
disp(xr);
if fx1(a)*fx1(xr) < 0
disp('The first root is in the first interval and the value of the root
is: ');
disp(fx1(a)*fx1(xr));
elseif fx1(b)*fx1(xr) < 0
disp('The first root is in the secont interval and the value of the
root is: ');
disp(fx1(b)*fx1(xr));
end;
iter = 0;
maxiter = 10;
while abs((b-a) / b) * 100 > toll
xprev = xr;
xr = (a + b) / 2;
if fx1(xr) == 0
disp('This is exact root');
elseif fx1(xr)*fx1(b) < 0
a = xr;
elseif fx1(xr) * fx1(a) < 0
b = xr;
end
iter = iter + 1;
if iter >= maxiter, break, end
abserror = abs(((xr - xprev) / xr) * 100);
display = ['The values for the ', num2str(iter), '. iteration
are: a: ', num2str(a), ' b: ', num2str(b), ' xr: ', num2str(xr), ', and the
error: ', num2str(abserror)];
disp(display);
end

Mehmed Duhovi
Numerical Analysis Homework
The values for the 1. iteration are: a: -0.5 b: 0 xr: -0.5, and the error: 0
The values for the 2. iteration are: a: -0.5 b: -0.25 xr: -0.25, and the error: 100
The values for the 3. iteration are: a: -0.5 b: -0.375 xr: -0.375, and the error:
33.3333
The values for the 4. iteration are: a: -0.4375 b: -0.375 xr: -0.4375, and the error:
14.2857
The values for the 5. iteration are: a: -0.4375 b: -0.40625 xr: -0.40625, and the
error: 7.6923
The values for the 6. iteration are: a: -0.42188 b: -0.40625 xr: -0.42188, and the
error: 3.7037
The values for the 7. iteration are: a: -0.42188 b: -0.41406 xr: -0.41406, and the
error: 1.8868
The values for the 8. iteration are: a: -0.41797 b: -0.41406 xr: -0.41797, and the
error: 0.93458
The values for the 9. iteration are: a: -0.41602 b: -0.41406 xr: -0.41602, and the
error: 0.46948

a = -1;
b= 0;
f =@(x)-12-21*x+18*x.^2-2.75*x.^3;
toll = 0.01;
iter = 0;
step = 5000000;
x = b - (f(b) * (b - a)) / (f(b) - f(a));
while (step >= toll)
xprev = x;
x = b - (f(b) * (b - a)) / (f(b) - f(a));
if abs(f(x)) <= toll
disp('iterations:');
disp(iter);
disp('x is');
disp(x);
break;
elseif f(a) * f(x) < 0
step = b - x;
b = x;
elseif f(xr) * f(a) < 0
step = x - a;
a = x;
end
iter = iter + 1;
abserror = abs(((x - xprev) / x) * 100);
display = ['The values for the ', num2str(iter), '. iteration are: a:
', num2str(a), ' b: ', num2str(b), ' xr: ', num2str(x), ', and the error:
', num2str(abserror)];
disp(display);

Mehmed Duhovi
Numerical Analysis Homework
end

The values for the 1. iteration are: a: -1 b: -0.28743 xr: -0.28743, and the error: 0
The values for the 2. iteration are: a: -1 b: -0.37945 xr: -0.37945, and the error:
24.252
The values for the 3. iteration are: a: -1 b: -0.40523 xr: -0.40523, and the error:
6.3626
The values for the 4. iteration are: a: -1 b: -0.41217 xr: -0.41217, and the error:
1.684

5.7.)

Bisection:
The values for the 1. iteration are: a: 1.25 b: 2 xr: 1.25, and the error: 0
The values for the 2. iteration are: a: 1.25 b: 1.625 xr: 1.625, and the error:
23.0769
The values for the 3. iteration are: a: 1.25 b: 1.4375 xr: 1.4375, and the error:
13.0435

Mehmed Duhovi
Numerical Analysis Homework
Regula Falsi:
The values for the 0. iteration are: a: 0.5 b: 1.6287 xr: 1.6287, and the error: 0
The values for the 1. iteration are: a: 0.5 b: 1.497 xr: 1.497, and the error: 8.7971
The values for the 2. iteration are: a: 0.5 b: 1.4484 xr: 1.4484, and the error:
3.3565
6.4.)

6.4.)
function [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end

Mehmed Duhovi
Numerical Analysis Homework
iter = 0;
xrold = xr;
while (1)
xprev = xr;
xrold = xr;
xr = xr - func(xr)/dfunc(xr);
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
abserror = abs(((xr - xprev) / xr) * 100);
display = ['The values for the ', num2str(iter), '. iteration are: xr: ',
num2str(xr), ', and the error: ', num2str(abserror)];
disp(display);
end

The
The
The
The

values
values
values
values

for
for
for
for

the
the
the
the

1.
2.
3.
4.

iteration
iteration
iteration
iteration

are:
are:
are:
are:

xr:
xr:
xr:
xr:

0.14438,
0.16941,
0.17018,
0.17018,

and
and
and
and

the
the
the
the

error:
error:
error:
error:

107.7907
14.7764
0.4529
0.00042175

function root = secant(func,xrold,xr,es,maxit)


% secant(func,xrold,xr,es,maxit):
% uses secant method to find the root of a function
% input:
% func = name of function
% xrold, xr = initial guesses
% es = (optional) stopping criterion (%)
% maxit = (optional) maximum allowable iterations
% output:
% root = real root
% if necessary, assign default values
if nargin<5, maxit=50; end %if maxit blank set to 50
if nargin<4, es=0.001; end %if es blank set to 0.001
% Secant method
iter = 0;
while (1)
xrn = xr - func(xr)*(xrold - xr)/(func(xrold) - func(xr));
iter = iter + 1;
if xrn ~= 0, ea = abs((xrn - xr)/xrn) * 100; end
if ea <= es | iter >= maxit, break, end
xrold = xr;
xr = xrn;
display = ['The values for the ', num2str(iter), '. iteration are: xr: ',
num2str(xrold), ', and the error: ', num2str(ea)];
disp(display);
end
root = xrn;

The
The
The
The
The
The

values
values
values
values
values
values

ans =

for
for
for
for
for
for

the
the
the
the
the
the

1.
2.
3.
4.
5.
6.

iteration
iteration
iteration
iteration
iteration
iteration

are:
are:
are:
are:
are:
are:

xr:
xr:
xr:
xr:
xr:
xr:

0.4, and the error: 14278.0243


0.002782, and the error: 98.7252
0.21824, and the error: 21.9274
0.17899, and the error: 5.5085
0.16964, and the error: 0.31829
0.17019, and the error: 0.0033704

Mehmed Duhovi
Numerical Analysis Homework
0.1702
6.5.)
Newton-Raphson:
The
The
The
The
The
The
The

values
values
values
values
values
values
values

for
for
for
for
for
for
for

the
the
the
the
the
the
the

1.
2.
3.
4.
5.
6.
7.

iteration
iteration
iteration
iteration
iteration
iteration
iteration

are:
are:
are:
are:
are:
are:
are:

xr:
xr:
xr:
xr:
xr:
xr:
xr:

2.3001, and the error: 74.675


90.0751, and the error: 97.4465
72.7152, and the error: 23.8738
58.8306, and the error: 23.601
47.727, and the error: 23.2648
38.8493, and the error: 22.8517
31.7535, and the error: 22.3465

Secant:
function root = modsec(func,xr,delta,es,maxit)
% secant(func,xrold,xr,es,maxit):
% uses the modified secant method
% to find the root of a function
% input:
% func = name of function
% xr = initial guess
% delta = perturbation fraction
% es = (optional) stopping criterion (%)
% maxit = (optional) maximum allowable iterations
% output:
% root = real root
% if necessary, assign default values
if nargin<5, maxit=50; end %if maxit blank set to 50
if nargin<4, es=0.001; end %if es blank set to 0.001
if nargin<3, delta=1E-5; end %if delta blank set to 0.00001
% Secant method
iter = 0;
while (1)
xrold = xr;
xr = xr - delta*xr*func(xr)/(func(xr+delta*xr)-func(xr));
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
display = ['The values for the ', num2str(iter), '. iteration are: xr: ',
num2str(xr), ', and the error: ', num2str(ea)];
disp(display);
end
root = xr;

The
The
The
The
The
The
The

values
values
values
values
values
values
values

for
for
for
for
for
for
for

the
the
the
the
the
the
the

1.
2.
3.
4.
5.
6.
7.

iteration
iteration
iteration
iteration
iteration
iteration
iteration

are:
are:
are:
are:
are:
are:
are:

xr:
xr:
xr:
xr:
xr:
xr:
xr:

1e-05, and the error: 100.001


-1.0046, and the error: 1219.4054
-0.076141, and the error: 39.3117
-0.12546, and the error: 41.2132
-0.21342, and the error: 7.398
-0.19872, and the error: 0.63086
-0.19998, and the error: 0.010408

Вам также может понравиться