HW3_Solutions

# HW3_Solutions - MAE 107 – Computational Methods Summer...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

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.

Unformatted text preview: MAE 107 – Computational Methods Summer Session II 2009 Homework # 3 Problem 1 – 10 points 1. FalsePos.m Solution: function [x,count] = FalsePos(F,a,b,er_final) % Performs bracketing test with the False Positive algorithm to find a root % of the functin F(x) over the interval [a,b] within the relative error % tolerence er. % Check if [a,b] is a true bracket if F(a)*F(b) >= 0 error([’The interval [’, num2str(a),’,’,num2str(b),’] is not a bracket!’]) end % Define an initial relative error er = 1; % Define an ititial guess of the root location x_old = F(b); % Setup iteration counter count = 0; while abs(er) > er_final % Define the false positive step x = b - (F(b)*(a-b))/(F(a)-F(b)); % if F(new point) has different sign than F(b) then make new bracket % [new_point,b], else make new bracket [a,new_point] if abs(F(x))<1e-14, % in case x is exactly at the root, to exit the loop break; elseif F(x)*F(b) < 0 a = x; else b = x; end 1 % Calculate the reative error er = (x - x_old)/x; % Redefine the old point as the newly calculated point for er % calculation on next iteration x_old = x; % Iterate the counter. count = count + 1; end Verifying the solution as requested: f ( x ) = x 2 − 5 over the interval [ , 4 ] (and with error 10 − 6 ) >> [x,count] = FalsePos(@(x) x.ˆ2-5,0,4,1e-6) x = 2.2361 count = 13 We can clearly see convergence toward √ 5 2. find roots.m Solution: function [x] = find_roots(F,A,B,Ns,er_final) % find_roots performs a successivly incremental search on the function F % over the interval [A,B] with Ns sub-sets. If a possible location is % found, the FalsePos function is called to find the root within a % relative tolerence of er_final. % Break the interval [A,B] into Ns subspaces; l = linspace(A,B,Ns+1); % Pre-define the output vector as a null x = ; % Check for a bracket for index = 1:length(l)-1 a = l(index) b = l(index+1); 2 % If F(a)*F(b) < 0 then we have a bracket if F(a)*F(b) < 0 % Now perform a FalsePos search [Xn,its] = FalsePos(F,a,b,er_final); % Store the root in a vector...
View Full Document

## This note was uploaded on 10/11/2011 for the course MAE 107 taught by Professor Rottman during the Summer '08 term at UCSD.

### Page1 / 8

HW3_Solutions - MAE 107 – Computational Methods Summer...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online