Homework #2--MATLAB Due 10/27/07

David Wagner 2007/09/27 17:10

Problem 1

%CE5143 Numerical Methods, Fall 2007
%Homework 2
%David Wagner

clear;
format long;

%First, define the starting interval and delta
interval_start=0.309000E+00; %Given: search start value
delta_n=0.1;                 %Given: starting delta
delta_n_min=0.0000001;       %Given: six decimal place accuracy
interval_divisions=10;

%Note if the solution is not within this starting interval,
% this m-file will display an incorrect value.


i=1:1:interval_divisions+1;
j=1:1:interval_divisions;

while(delta_n > delta_n_min)
%Successfully exit the loop when
% delta is accurate enough

%Make a vector of the points in the interval to test
n(i) = interval_start+((i-1)*delta_n);

%Make a vector of the function value at each test point
f(i)=0.9 .*((n(i)+1)./(3 .*n(i)+1)) + 0.8 .^((n(i)+1)./(n(i)))-1;

% Find the first index of where a sign change occurs
% If there is more than one, oh well;
% this will only catch the first change
i_at_change= -min(-j.*(sign(f(j))~= sign(f(j+1))));

%Now, prepare for the next iteration
% by creating a smaller division starting
% with the n-value immediately before the sign change
delta_n=delta_n/10;
interval_start=n(i_at_change);

end

disp (interval_start);

return ;

Sample Ouput:

>> clear
   0.309162300000000

>> 

Problem 2

%CE5143 Numerical Methods, Fall 2007
%Homework 2
%David Wagner

clear;
format long;

epsilon=0.0000001;

max_tries=20;
i=1:1:max_tries+1;

x(i)=0;
f(i)=epsilon+1; %start larger than epsilon
k=2;
while((k < max_tries) & (abs(f(k-1)) > epsilon))
    f(k) = x(k).^3 -0.2589.*x(k).^2 +0.02262.*x(k) -0.001122;
    f_prime(k) = 3.*x(k).^2 -0.5178.*x(k) +0.02262;
    x(k+1)=x(k) - (f(k) ./ f_prime(k));
    k=k+1;
end

disp(x(k-1));

Sample Output

>> clear
   0.162038359758825

Personal Tools