0% found this document useful (0 votes)
75 views13 pages

Ce 335

The document contains MATLAB code for implementing and comparing the performance of the inverse function on matrices of increasing sizes from 5x5 to 500x500. Random permutation matrices of each size are generated. The inverse of each matrix is then calculated using the tic and toc commands to measure the time taken. The times are stored in a vector t and plotted against the matrix sizes in vector n to analyze how computation time increases with size.

Uploaded by

Amarilis Ramos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views13 pages

Ce 335

The document contains MATLAB code for implementing and comparing the performance of the inverse function on matrices of increasing sizes from 5x5 to 500x500. Random permutation matrices of each size are generated. The inverse of each matrix is then calculated using the tic and toc commands to measure the time taken. The times are stored in a vector t and plotted against the matrix sizes in vector n to analyze how computation time increases with size.

Uploaded by

Amarilis Ramos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy