Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
HW 4 Update and Revisions
- Loading branch information
Showing
4 changed files
with
93 additions
and
10 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
%HW_4 Script | ||
|
||
setdefaults; | ||
|
||
%Part B: using the goldmin function | ||
theta = 0; | ||
[x1,fx1,ea1,iter1] = goldmin(@(x) collar_potential_energy(x,theta),-10,10,0.0001); | ||
|
||
%Part C: Creating For-loop solving for potential energy | ||
|
||
theta = .9*pi/180; | ||
thetaVector = linspace(0,pi, 400); | ||
fx2 = 0; | ||
|
||
[x3,fx3,ea3,iter3] = goldmin(@(x) collar_potential_energy(x,theta),-10,10,0.0001); | ||
|
||
for n = 1:length(thetaVector) | ||
theta = thetaVector(n); | ||
[x2,fx2(n),ea2,iter2] = goldmin(@(x) collar_potential_energy(x,theta),-10,10,0.0001); | ||
end | ||
|
||
%Part D: Plotting the Data | ||
|
||
plot(thetaVector, fx2); | ||
title('Plot of Minimum PE for x_C with given Angle'); | ||
xlabel('Theta (radians)'); | ||
ylabel('x_C (meters)'); | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function [ PE_tot ] = collar_potential_energy( x_C, theta ) | ||
|
||
% Computes total Potential energy of the collar system given | ||
% an initial x_C and theta | ||
|
||
m = 0.5; % mass in kg | ||
g = 9.81; % acceleration due to gravity m/s^2 | ||
K = 30; %spring constant N/m | ||
|
||
PE_g = m*x_C*g*sin(theta); | ||
PE_s = .5*K *((0.5 - sqrt(0.5^2+(0.5-x_C)^2)))^2; | ||
|
||
PE_tot = PE_g + PE_s; | ||
|
||
end | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function [x,fx,ea,iter]=goldmin(f,xl,xu,es,maxit,varargin) | ||
% goldmin: minimization golden section search | ||
% [x,fx,ea,iter]=goldmin(f,xl,xu,es,maxit,p1,p2,...): | ||
% uses golden section search to find the minimum of f | ||
% input: | ||
% f = name of function | ||
% xl, xu = lower and upper guesses | ||
% es = desired relative error (default = 0.0001%) | ||
% maxit = maximum allowable iterations (default = 50) | ||
% p1,p2,... = additional parameters used by f | ||
% output: | ||
% x = location of minimum | ||
% fx = minimum function value | ||
% ea = approximate relative error (%) | ||
% iter = number of iterations | ||
if nargin<3,error('at least 3 input arguments required'),end | ||
if nargin<4|isempty(es), es=0.0001;end | ||
if nargin<5|isempty(maxit), maxit=50;end | ||
phi=(1+sqrt(5))/2; | ||
iter=0; | ||
while(1) | ||
d = (phi-1)*(xu - xl); | ||
x1 = xl + d; | ||
x2 = xu - d; | ||
if f(x1,varargin{:}) < f(x2,varargin{:}) | ||
xopt = x1; | ||
xl = x2; | ||
else | ||
xopt = x2; | ||
xu = x1; | ||
end | ||
iter=iter+1; | ||
if xopt~=0, ea = (2 - phi) * abs((xu - xl) / xopt) * 100;end | ||
if ea <= es | iter >= maxit,break,end | ||
end | ||
x=xopt;fx=f(xopt,varargin{:}); |