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/HW_3 Script
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
78 lines (61 sloc)
3.11 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
%HW_3 | |
%Script calls functions and outputs tables and plots relevant to HW3. | |
[root(1),fx(1),ea(1),iter(1)]=falsepos(@(x) projectile_HW3(x),1,50,.00001); | |
[root(2),ea(2),iter(2)]=mod_secant(@(x) projectile_HW3(x),1,50,.00001); | |
[root(3), ea(3), iter(3)]= newtraph(@(x) projectile_HW3(x),@(x) dh_dtheta(x),50,.00001,1000); | |
[root(4),fx(4),ea(4),iter(4)]=bisect(@(x) projectile_HW3(x),0,50,.00001); | |
for k=1:4; | |
Table(k,1)=root(k); | |
Table(k,2)=ea(k); | |
Table(k,3)=iter(k); | |
Table(k,4)=fx(k); | |
end | |
%Root=[root(1);root(2);root(3);root(4)]; | |
%Initial_Guess=[50;50;50;50] | |
%Ea=[ea(1);ea(2);ea(3);ea(4)]; | |
%Iter=[iter(1);iter(2);iter(3);iter(4)]; | |
%Labels={'Falsepos','Mod_Secant','Bisect','Newtraph'}; | |
%table(Initial_Guess,Root,Ea,Iter,'RowNames',Labels) | |
%This would be the table function used to create a table in MATLAB 2016a. | |
%Unfortunately in my 2013 MATLAB version there is no table function. This | |
%stores values for each column of the table and I saved the row labels as a | |
%matrix. The table function puts it in a table. | |
fprintf('\n\n\n'); | |
fprintf('Solver Initial Guess Root Ea Iterations\n'); | |
fprintf('Falsepos 50 %4.4f %4.2s %4.0f\n', Table(1,1),Table(1,2),Table(1,3)) | |
fprintf('Bisect 50 %4.4f %4.2s %4.0f\n', Table(2,1),Table(2,2),Table(2,3)) | |
fprintf('Mod_Secant 50 %4.4f %4.2s %4.0f\n', Table(3,1),Table(3,2),Table(3,3)) | |
fprintf('Newtraph 50 %4.4f %4.2s %4.0f\n', Table(4,1),Table(4,2),Table(4,3)) | |
fprintf('\n\n\n\n'); | |
for k=1:4; | |
iter(k)=log10(iter(k)); | |
end | |
scatter(iter,ea) | |
for k=1:4 | |
points(1,k)=iter(1,k); | |
points(2,k)=ea(1,k); | |
end | |
labels={'Falsepos','Bisect', 'Mod Secant', 'Newtraph'}; | |
text(points(1,:),points(2,:),labels,'horizontal','left','vertical','bottom'); | |
xlabel('Iterations (log10 base)'); | |
ylabel('Relative Accuracy'); | |
title('Roots and Optimization'); | |
[x_i,Iter,Ea]=newtraph_edit(@(x) Y_Function_HW3(x),@(x) Derivative_HW3(x),2,.00001,5); | |
fprintf('Divergence of Newton-Raphson Method\n'); | |
fprintf('Iteration x_i Approximate Error\n'); | |
fprintf('%4.0f %4.4f %4.2s\n', Iter(1),x_i(1),Ea(1)); | |
fprintf('%4.0f %4.4f %4.2s\n', Iter(2),x_i(2),Ea(2)); | |
fprintf('%4.0f %4.4f %4.2s\n', Iter(3),x_i(3),Ea(3)); | |
fprintf('%4.0f %4.4f %4.2s\n', Iter(4),x_i(4),Ea(4)); | |
fprintf('%4.0f %4.4f %4.2s\n', Iter(5),x_i(5),Ea(5)); | |
fprintf('%4.0f %4.4f %4.2s\n', Iter(6),x_i(6),Ea(6)); | |
[x_i,Iter,Ea]=newtraph_edit2(@(x) Y_Function_HW3(x),@(x) Derivative_HW3(x),.2,.00001,5); | |
fprintf('\n\n\n'); | |
fprintf('Convergence of Newton-Raphson Method\n'); | |
fprintf('Iteration x_i Approximate Error\n'); | |
fprintf('%4.0f %4.6f %4.2s\n', Iter(1),x_i(1),Ea(1)); | |
fprintf('%4.0f %4.6f %4.2s\n', Iter(2),x_i(2),Ea(2)); | |
fprintf('%4.0f %4.6f %4.2s\n', Iter(3),x_i(3),Ea(3)); | |
fprintf('%4.0f %4.6f %4.2s\n', Iter(4),x_i(4),Ea(4)); | |
fprintf('%4.0f %4.6f %4.2s\n', Iter(5),x_i(5),Ea(5)); | |
fprintf('%4.0f %4.6f %4.2s\n', Iter(6),x_i(6),Ea(6)); | |