This preview shows pages 1–4. Sign up to view the full content.
Contents
•
Problem 1
•
Problem 2
•
Problem 3
clear all; close all; clc
format compact
Problem 1
% A) They are different because there is varation in execution time
% B) For Loop
n=[1 5 10 20 40 65 100];
for a=1:length(n)
for i = 1:10
A = zeros(1,n(a));
tic
for j=1:n(a)
A(j) = j^2;
end
T(i,a) = toc;
clear A
end
end
plot(n,T, 'rx'); hold on; plot(n,mean(T)); hold off;
% C) The Time complexity of this function is Linear
% D) Matrix Opperation
for a=1:length(n)
for i = 1:10
A = zeros(1,n(a));
tic
A = [1:n(a)].^2;
T(i,a) = toc;
clear A
end
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document end
figure; plot(n,T, 'rx'); hold on; plot(n,mean(T)); hold off;
% E) The second method is better because it calculates faster
clear all; clc;
% % Fibinach Numbers
n = 1:3:22;
% A) Recursion
for i=1:length(n)
for j=1:5
tic;
fib1(n(i));
Ta(j,i) = toc;
end
end
% Exponential Time Complexity
type fib1
%B) For Loop
for i=1:length(n)
for j=1:5
tic;
fib2(n(i));
Tb(j,i) = toc;
end
end
% Linear Time Complexity
type fib2
% C) Direct
ph = .5*(1+5^.5);
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
This document was uploaded on 01/21/2011.
 Fall '09

Click to edit the document details