Write MATLAB codes to find the roots of the following equations. Submit your cod
ID: 3871068 • Letter: W
Question
Write MATLAB codes to find the roots of the following equations. Submit your codes and outputs as a single pdf file to sakai. Be carefull There may be multiple roots. In all problems above, set a tolerance of 10 for convergence. 1. sin(x2+x2 -2x-0.09-0 using Newton's Method, and using MATLAB's fzero function cos4w-x2)-8x2+8x4+1=0 using Bisection Method to find the brackets, and 2. then Secant Method to find all the roots. So this is like a hybrid method. 3. x-3x +2.25-0 using Newton's Method. How many iterations you need to get a converged result here? Compare this with the number of iterations you got in HW 3 for this problem using Fixed Point iterations (using the same initial guess).Explanation / Answer
1.
fzero function is different method altogether. It is combination of secant, bisection and backward interpolation.
We solve using newton's method.
function [ x, err_est ] = newton( func, diff_func, x0, tolerance, nmaxiter )
%
% Input:
% func - input funtion
% diff_func - derived input function
% x0 - inicial aproximation
% tolerance - tolerance
% nmaxiter - maximum number of iterations
%
% Output:
% x - aproximation to root
% err_est - error estimate
%
% Example:
% [ x, ex ] = newton( 'sin(x.^2)+x.^2-2.*x-0.09', '2.*x.*cos(x.^2)+2.*x-2', 0, 10^-8, 100)
%
% Result:
%x =
% -0.0450 -0.0431 -0.0431 -0.0431
%ex =
% 0.0450 0.0019 0.0000 0.0000
%Answer:-0.0431
if nargin == 3
tolerance = 1e-4;
nmaxiter = 1e1;
elseif nargin == 4
nmaxiter = 1e1;
elseif nargin ~= 5
error('newton: invalid input parameters');
end
func = inline(func);
diff_func = inline(diff_func);
x(1) = x0 - (func(x0)/diff_func(x0));
err_est(1) = abs(x(1)-x0);
k = 2;
while (err_est(k-1) >= tolerance) && (k <= nmaxiter)
x(k) = x(k-1) - (func(x(k-1))/diff_func(x(k-1)));
err_est(k) = abs(x(k)-x(k-1));
k = k+1;
end
end
2.
function b_out = hybrid(func,a_in,b_out)
fa = func(a_in);
fb = func(b_out);
s = '%5.0f %8s %19.15f %23.15e ';
fprintf(s,1,'initial',a_in,fa)
fprintf(s,2,'initial',b_out,fb)
k = 2;
% a_in is the previous value of b_out and [b_out, c] always contains the zero.
c = a_in; fc = fa;
while true
if sign(fb) == sign(fc)
c = a_in; fc = fa;
end
% Swap to insure f(b) is the smallest value so far.
if abs(fc) < abs(fb)
a_in = b_out; fa = fb;
b_out = c; fb = fc;
c = a_in; fc = fa;
end
% Midpoint.
m = (b_out + c)/2;
if abs(m - b_out) <= eps(abs(b_out))
return % Exit from the loop and the function here.
end
% p/q is the the secant step.
p = (b_out - a_in)*fb;
if p >= 0
q = fa - fb;
else
q = fb - fa;
p = -p;
end
% Save this point.
a_in = b_out; fa = fb;
k = k+1;
% Choose next point.
if p <= eps(q)
% Minimal step.
b_out = b_out + sign(c-b_out)*eps(b_out);
fb = func(b_out);
fprintf(s,k,'minimal',b_out,fb)
elseif p <= (m - b_out)*q
% Secant.
b_out = b_out + p/q;
fb = func(b_out);
fprintf(s,k,'secant ',b_out,fb)
else
% Bisection.
b_out = m;
fb = func(b_out);
fprintf(s,k,'bisect ',b_out,fb)
end
end
end
%Result
hybrid(f,-10,10)
1 initial -10.000000000000000 3.515534819284370e+172
2 initial 10.000000000000000 3.515534819284370e+172
3 bisect 0.000000000000000 2.000000000000000e+00
4 minimal 0.000000000000000 2.000000000000000e+00
ans =
4.9407e-324
3.
[ x, ex ] = newton( 'x.^6-3.*x.^3+2.25', '6.*x.^5-9.*x.^2', -100, 10^-8, 40)
x =
Columns 1 through 12
-83.3333 -69.4444 -57.8703 -48.2252 -40.1875 -33.4894 -27.9076 -23.2561 -19.3796 -16.1490 -13.4565 -11.2124
Columns 13 through 24
-9.3417 -7.7819 -6.4808 -5.3947 -4.4870 -3.7267 -3.0876 -2.5468 -2.0838 -1.6789 -1.3104 -0.9464
Columns 25 through 36
-0.5095 0.5382 1.3115 1.2383 1.1949 1.1709 1.1581 1.1515 1.1481 1.1464 1.1456 1.1451
Columns 37 through 40
1.1449 1.1448 1.1448 1.1447
ex =
Columns 1 through 12
16.6667 13.8889 11.5741 9.6451 8.0376 6.6981 5.5818 4.6516 3.8765 3.2306 2.6925 2.2441
Columns 13 through 24
1.8707 1.5598 1.3011 1.0861 0.9077 0.7602 0.6391 0.5408 0.4630 0.4049 0.3685 0.3640
Columns 25 through 36
0.4369 1.0478 0.7732 0.0732 0.0433 0.0241 0.0128 0.0066 0.0034 0.0017 0.0009 0.0004
Columns 37 through 40
0.0002 0.0001 0.0001 0.0000
Converges at 40th iteration
Fixed Point iteration
format long;
%% set initial guess %%
z(1) = -100;
for n = 1:40
z(n+1) = z(1).^6-3.*z(1).^3+2.25;
x(n) = z(n+1)
end
%Result
1.0e+12 *
Columns 1 through 6
1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250
Columns 7 through 12
1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250
Columns 13 through 18
1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250
Columns 19 through 24
1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250
Columns 25 through 30
1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250
Columns 31 through 36
1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250
Columns 37 through 40
1.000003000002250 1.000003000002250 1.000003000002250 1.000003000002250
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.