The purpose of this problem is to review the application of programming in solvi
ID: 3801301 • Letter: T
Question
The purpose of this problem is to review the application of programming in solving nonlinear equations. Consider a nonlinear algebraic equation f(r) 0 which does not have an analytical so- lution. In this case, to find the solution we can use computational methods. For example consider the following function: f (r) sin(r) To find the root of flac) 0, we can use the method of successive substitution for more details, read Dunn et al., Numerical Methods in Biomedical Engineering, page 124). In this method, we rearrange our equations into r g(r), or in the case of the example above g(z) (r) sun. In the next step we pick an initial guess To and calculate T1 g(ro) T1 If gr (To)I 1, T1 should be closer to the root than our initial guess. A general iterative formula for this method is g (rn) n+1 Let us use this method to find the roots of sin (T) 0. As stated previously g(z) sin (r) a. We set our initial guess co to be 1. The successive substitution method gives the following values for T at each iteration step sin (zo) Iteration 1 1.365069760 0.365069760 To sin (r1) Iteration 2 1.502511597 0.137441836 Iteration 3 T3 sin(r2) 1.521268279 Iz3 T2 0.018756683Explanation / Answer
Part a Matlab function MainProblem2a.m
function [y,k]= MainProblem2a(g)
% g is the matlab function handler
k = 0; % loop counter initially set to zero
x0 = 1; % Initial guess
Del = 1; % Some initial value for error
tol = 10^-6;
while Del > tol
x = g(x0); % Computing the value of g at x0
Del = abs(x-x0); % Absolute difference between two iteration val of x
x0 = x; % Updating the value of x0 for the next iteration
k = k+1; % increasing the loop count
end
y = x0; % Copy the value of x0 if converges to the root
end
Testing the function
>> g = @(x) cos(x).^2+pi/3;
>> [y,k]= MainProblem2a(g)
y =
1.1872
k =
36
Function modified for Problem b
function [y,k]= MainProblem2a(g)
% g is the matlab function handler
k = 0; % loop counter initially set to zero
x0 = 1; % Initial guess
Del = 1; % Some initial value for error
tol = 10^-6; % Tolerence
Max_Iter = 200; % Maximum number of iterations allowded
while Del > tol && k > Max_Iter % Nuber of iteration exceed 100 stop iteration
x = g(x0); % Computing the value of g at x0
Del = abs(x-x0); % Absolute difference between two iteration val of x
x0 = x; % Updating the value of x0 for the next iteration
k = k+1; % increasing the loop count
end
if k > Max_Iter % if iteration exceed Max_Iter implice Method faild to converge
error('Method Faild to converge: Maximum number of iterations reached');
end
y = x0; % Copy the value of x0 if converges to the root
end
Testing the function for Problem b
>> g =@(x) (x.^2).*cos(x)+pi/3;
>> [y,k]= MainProblem2a(g)
Error using MainProblem2a (line 15)
Method Faild to converge: Maximum number of iterations reached
Remakrs: The solutions are in a loop. that is if X1 = f(X2) In the next iteration we are getting X2 = g(X1). It is repeating again and again. So the solution is not convergring
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.