Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
roots_and_optimization/HW3_Work
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
59 lines (46 sloc)
1.74 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%Question 1 | |
v = 15; | |
g = -9.81; | |
height = @(theta) 2.37*tan(theta) + .5*g.*((2.37./(v*cos(theta))).^2); %eq in terms of theta | |
dh_dtheta = @(theta) 2.37.*(sec(theta).^2) + (g*2.37^2.*(sec(theta).^2).*tan(theta))./(v^2); %derivative of equation | |
%begin eval four methods | |
[root1,fx1,ea1,iter1] = bisect(height,0,.1,0.00001,100); | |
[root2,fx2,ea2,iter2] = falsepos(height,0,.1,0.00001,100); | |
[root3,ea3,iter3] = newtraph(height,dh_dtheta,.1,0.00001,100); | |
[root4,ea4,iter4] = mod_secant(height,.0001,.1,0.00001,100); | |
%Table of answers | |
t_iter = [ iter1 iter2 iter3 iter4]; | |
ig = { .1 .1 'Na' 'Na' }; | |
ea = [ ea1 ea2 ea3 ea4 ]; | |
%Make Table | |
T = table; | |
T.Solver = {'bisect', 'falsepos','newtraph', 'mod_secant'}'; | |
T.Initial_Guess = ig'; | |
T.ea = ea'; | |
T.Iterations = t_iter'; | |
T | |
%function for plotting | |
e_b = zeros(length(iter1)); | |
e_f = zeros(length(iter2)); | |
e_n = zeros(length(iter3)); | |
e_m = zeros(length(iter4)); | |
%Creating vectors for error approximation vs iteration | |
for c = 1:iter1 | |
[r, y, e_b(c), k] = bisect(height,0,.1,0.00001,c); | |
end | |
for c = 1:iter2 | |
[r, y, e_f(c), k] = falsepos(height,0,.1,0.00001,c); | |
end | |
for c = 1:iter3 | |
[r, e_n(c), k] = newtraph(height,dh_dtheta,.1,0.00001,c); | |
end | |
for c = 1:iter4 | |
[r, e_m(c), k] = mod_secant(height,.0001,.1,0.00001,c); | |
end | |
%Ploting | |
setdefaults | |
plot(1:iter1, e_b, 'g', 1:iter2, e_f, '--', 1:iter3, e_n, 'c:', 1:iter4, e_m, 'o') | |
title('Approximate Error of Convergent Functions versus Number of Iterations'); | |
xlabel('Iteration'); | |
ylabel('Approx Error'); | |
legend('bisect','falsepos', 'newtraph', 'mod secant'); |