Uploaded by doveagro256

Matlab Assignment - Copy-1

advertisement
KYAMBOGO
UNIVERSITY
P.O.BOX 1, KYAMBOGO – KAMPALA- UGANDA
TEL: +256-41-4287340 www.kyambogo.ac.ug
FACULTY OF ENGINEERING
DEPARTMENT OF CIVILAND BUILDING ENGINEERING
ASSIGNMENT FOR TCEM 2101, ENGINEERING MATHEMATICS III
LECTURER: MR. ENYOGOI ISAAC
GROUP MEMBERS
Name
Registration Number
BUKYANA GEOFREY
18/U/ECD/9063/PD
NAMARA DEAN DANIEL
AJAO JOAN OPIO
SESAGI NESTA
Signature
Secant Method.
%%inital guesses
x0=2;
x1=3;
%% writing the function
functionx = @(x) 2*x^2-3;
%%setting the tolerance
tolerance= 1.0e-6;
tic %%setting the time
for i= 1:75
%%formula for secant method
xn= ((x0*functionx(x1))-(x1*functionx(x0)))/(functionx(x1)-functionx(x0));
fprintf('iterations: %d\tx1: %f\txn:%f\n',i,x1,xn)
%%checking for the tolerance
if abs(functionx(x1)-functionx(xn))>= tolerance
x0=x1;
x1=xn;
else
break
end
end
toc
%%displaying the root and number of iterations
fprintf('Root =: %f\nIterations: %d \n',xn,i)
iterations=1:7;
xn=[1.500000 1.333333 1.235294 1.225191 1.224747 1.224745 1.224745];
x1=[3.000000 1.500000 1.333333 1.235294 1.225191 1.224747 1.224745];
figure(2);
plot(iterations,xn,'-r*');
hold on;
plot(iterations,x1,'--bo');
axis([1,7,1.224745,3.000000]);
title('TESTING FOR CONVERGENCE USING SECANT METHOD');
xlabel('iterations');
ylabel('xn & x1');
legend('xn','x1');
box on;
legend('boxoff');
grid on;
hold off;
>>
iterations: 1
x1: 3.000000 xn:1.500000
iterations: 2
x1: 1.500000 xn:1.333333
iterations: 3
x1: 1.333333 xn:1.235294
iterations: 4
x1: 1.235294 xn:1.225191
iterations: 5
x1: 1.225191 xn:1.224747
iterations: 6
x1: 1.224747 xn:1.224745
iterations: 7
x1: 1.224745 xn:1.224745
Elapsed time is 0.005478 seconds.
Root =: 1.224745
Iterations: 7
>>
Bisection Method
x1=0;
x2=4;
%%writing the function
y =@(x) x^3-4*x-3;
%%setting the tolerance
err= 1.0e-4;
initial_iter=0;
final_iter=100;
total_xn= zeros(1,initial_iter);
%%setting time function to get the Elapsed time
tic
%%looping through the function to get the root
for iterations=initial_iter:final_iter
%%computing for xn
xn= (x1+x2)/2;
initial_iter=initial_iter +1;
total_xn(initial_iter)=xn;
%%checking the root lies in the interval of x1 and xn
if (y(x1)*y(xn))>0
x1=xn;
else
x2=xn;
end
%% condition of approach to solution
if abs(y(x1)-y(x2)) < 1.0e-6
disp('The root to the function(x^3-4x-3) is:')
disp(xn)
disp('The Number of bisections: ')
disp(iterations)
break % terminate the for-loop
end
end
%%closing the time function for finding the Elapsed time
toc
iterations=1:26;
total_xn= total_xn(1:initial_iter);
disp(total_xn)
figure(1);
plot(iterations,total_xn,'-m*');
axis([1,26,2.0000,3.0000]);
xlabel('iteration');
ylabel('xn');
title('TEST FOR CONVERGENCE OF "x^3-4x-3" USING BISECTION METHOD');
legend('xh');
box on;
grid on;
legend('boxoff');
hold off;
>>
The root to the function(x^3-4x-3) is:
2.3028
The Number of bisections:
25
Elapsed time is 0.006636 seconds.
Columns 1 through 15
2.0000 3.0000 2.5000 2.2500 2.3750 2.3125 2.2813 2.2969 2.3047 2.3008 2.3027 2.3037 2.3032 2.3030 2.3029
Columns 16 through 26
2.3028 2.3028 2.3028 2.3028 2.3028 2.3028 2.3028 2.3028 2.3028 2.3028 2.3028
>>
Newton Raphson Method
f =@(x) x^3-2*x-1; %setting the function
df=@(x) 3*x^2-2;%setting the derivation
x=2; %taking 2 as the initial approximation
err= 1.0e-6; %selecting the tolerance
tic %opening the time command
for iterations=1:100
xn=x-(f(x)/df(x)); %newton raphson formula
fprintf('iterations: %d\tx: %f\txn:%f\n',iterations,x,xn)
if abs(f(x)-f(xn))>= err %checking for the tolerance
x=xn;
else
break
end
end
toc %closing time function
fprintf('Root is to the equation is: %f\nIt iterates: %d times\n',xn,iterations)
iterations=1:5;
x=[2.000000 1.700000 1.623088 1.618055 1.618034];
xn=[1.700000 1.623088 1.618055 1.618034 1.618034];
figure(2);
plot(iterations,x,'-r*');
hold on;
plot(iterations,xn,'--bh');
axis([1,5,1.618034,2.000000]);
title('TESTING FOR CONVERGENCE USING NEWTON RAPHSON METHOD');
xlabel('iterations');
ylabel('x & xn');
legend('x','xn');
box on;
legend('boxoff');
grid on;
hold off;
>>
iterations: 1
x: 2.000000
xn:1.700000
iterations: 2
x: 1.700000
xn:1.623088
iterations: 3
x: 1.623088
xn:1.618055
iterations: 4
x: 1.618055
xn:1.618034
iterations: 5
x: 1.618034
xn:1.618034
Elapsed time is 0.004236 seconds.
Root is to the equation is: 1.618034
It iterates: 5 times
>>
Download