You are on page 1of 13

Salman Zafar

CE 335
Prof. Krakauer
H.W. # 2
%3.1 - Bisection Method

a=1;
b=2;
n=5; %5 iterations
fa = a^4-2;
fb = b^4-2;
if fa*fb > 0
error('Function has same sign at both endpoints a and b.')
end
disp(' x f(x) abs_error frac_error')
for i = 1:n
c = (a + b)/2;
fc = c^4-2;
abs_error=abs(c-(sqrt(2))); %absolule error
frac_error= abs_error/(sqrt(2)); %fractional error

disp([ c fc abs_error frac_error])


%The above displays all the values that are asked to be shown.

if fc == 0 % solved the equation exactly


e = 0;
return % stops iterating and returns; outputs
end
if fa*fc < 0
b=c;
else
a=c;
end
end
e = (b-a)/2; %this is the absolute error
Output:

%3.1 - Secant Method


a=1;
b=2;
n=5; %5 iterations
fa = a^4-2;
fb = b^4-2;
i=1;
disp(' x f(x) abs_error frac_error')
for i = 1:n
s =(fb - fa)/(b - a); %this is the slope estimate
c = b - fb/s; % secant formula
fc = c^4-2;
abs_error=abs(c-(sqrt(2))); %absolule error
frac_error= abs_error/(sqrt(2)); %fractional error

disp([c fc abs_error frac_error ])


%The above displays all the values that are asked to be shown.
a = b;
fa = fb;
b = c;
fb = fc;
i=i+1;
end
Output:
%3.1 - False Position Methood
a=1;
b=2;
n=5;
fa=a^4-2;
fb=b^4-2;
if fa*fb > 0
error('Function has same sign at both endpoints a and b.')
end
disp(' x f(x) abs_error frac_error')

for i=1:n
c=(b*fa-a*fb)/(fa-fb);
fc=c^4-2;
abs_error=abs(c-(sqrt(2)));
frac_error= abs_error/(sqrt(2));

disp([ c fc abs_error frac_error ])


%The above displays all the values that are asked to be shown.

if fa*fc==0
return %not helpful in finding roots, since product is zero
end;

if fa*fc >0
a=c;
else
if fa*fc <0
b=c;
end
end
end

Output:
function [c] = falsepos( a,b,n )
format long
format compact
a=1;
b=2;
n=5; %number of iterations are not specified, I chose 5
fa=a^4-2;
fb=b^4-2;
if fa*fb > 0
error('Function has same sign at both endpoints a and b.')
end
disp(' x f(x) abs_error frac_error')
for i=1:n
c=(b*fa-a*fb)/(fa-fb);
fc=c^4-2;
abs_error=abs(c-(sqrt(sqrt(2))));
frac_error= abs_error/(sqrt(sqrt(2)));
disp([ c fc abs_error frac_error ])
if fa*fc==0
return
end;
if fa*fc >0
a=c;
else
if fa*fc <0
b=c;
end
end
end
Output:
function [c] = secant_while(a,b, tol)
format long
format compact
a=1;
b=2;
tol= input('What is the tolerance?')

fa = a^4-2;
fb = b^4-2;
abs_error= 1;
disp(' x f(x) absolute_error
fractional_error')
while abs_error > tol
s =(fb - fa)/(b - a); %the estimation of the slope
c = b - fb/s; % secant formula.
fc = c^4-2; % function at the new approximate solution.
abs_error=abs(c-(sqrt(2))); %absolule error
frac_error= abs_error/(sqrt(2)); %fractional error
disp([c fc abs_error frac_error])
a = b;
fa = fb;
b = c;
fb = fc;
end
Output:

function [x] = symnewton(f,f1,n)


n=5; %chosen number of iterations
a=0.5; %initial value given in problem
syms x
f= log(x) + 1; %the function for usage, given in problem
f1=diff(f);
for i= 1:n
%to get value in a one number form, we use double
af= double(subs(f,a))
af1= double(subs(f1,a))
a=a- af/af1 %Newtons formula
end
Output:
M = [-2 0 1; 0 1 0; -1 1 0; 1 0 1];
N = [0 -1 1 0; 0 1 0 0; -1 2 -1 1];
M*N
N*M
inv(M)
%The inverse of a matrix can only be achieved if its number of rows equal
%its number of columns. In this case, it does not.

Output:
%4.3 %5 50 100 150 200 300 400 500
A= zeros(5,5);
for i1 = 1:5,
A(:,i1) = (randperm(5));
end

B = zeros(50,50);
for i2 = 1:50,
B(:,i2) = (randperm(50));
end

C = zeros(100,100);
for i3 = 1:100,
C(:,i3) = (randperm(100));
end

D = zeros(150,150);
for i4 = 1:150,
D(:,i4) = (randperm(150));
end

E = zeros(200,200);
for i5 = 1:200,
E(:,i5) = (randperm(200));
end

F = zeros(300,300);
for i6 = 1:300,
F(:,i6) = (randperm(300));
end

G = zeros(400,400);
for i7 = 1:400,
G(:,i7) = (randperm(400));
end

H = zeros(500,500);
for i8 = 1:500,
H(:,i8) = (randperm(500));
end

tic
inv(A)
timeneededA = toc
tic
inv(B)
timeneededB = toc

tic
inv(C)
timeneededC = toc

tic
inv(D)
timeneededD = toc

tic
inv(E)
timeneededE = toc

tic
inv(F)
timeneededF = toc

tic
inv(G)
timeneededG = toc

tic
inv(H)
timeneededH = toc

t = [timeneededA timeneededB timeneededC timeneededD timeneededE timeneededF


timeneededG timeneededH]
n = [5 50 100 150 200 300 400 500]
plot(t,n)
xlabel('Time Taken')
ylabel('Matrix Size')

Output:

You might also like