Question

Computer Problem. Solve fi(x) = x – cos(x) = 0 and f2(x) = x2 – 2xcos(x) + cos²(x) = 0 both with initial guess xo = 0 by usinPlease use MATLAB to solve the question

0 0
Add a comment Improve this question Transcribed image text
Answer #1

MATLAB Script:

close all
clear
clc

syms x
f1 = x - cos(x);
f2 = x^2 - 2*x*cos(x) + (cos(x))^2;
f1d = diff(f1); % f1'(x)
f2d = diff(f2); % f2'(x)

tols = 10 .^ -[5 6 8 10];
x0 = 0;
results_1 = zeros(1, length(tols));
results_2 = zeros(1, length(tols));
num_iters_1 = zeros(1, length(tols));
num_iters_2 = zeros(1, length(tols));
for i = 1:length(tols)
tol = tols(i);
[results_1(i), num_iters_1(i)] = my_newton(f1, f1d, x0, tol);
[results_2(i), num_iters_2(i)] = my_newton(f2, f2d, x0, tol);
end

fprintf('Tolerance\t\tIterations (f1)\t\tRoot (f1)\t\tIterations (f2)\t\tRoot (f2)\n')
for i = 1:length(tols)
fprintf('%e\t%d\t\t\t\t\t%.10f\t%d\t\t\t\t\t%.10f\n', tols(i), num_iters_1(i), results_1(i), num_iters_2(i), results_2(i))
end

function [res, num_iter] = my_newton(func,func_d,ic,tol)
res = ic;
num_iter = 0;
while true
num_iter = num_iter + 1;
x_ = res; % Previous value of approximate root
res = double(res - subs(func, res)/subs(func_d, res)); % Newton update rule
if abs(x_ - res) < tol
break;
end
end
end

Output:

Iterations (f1) Command Window Tolerance 1.000000e-955 1.000000-06 1.000000e-08 1.000000e-10 fx >> Root (fi) 0.7390851332 0.7

Add a comment
Know the answer?
Add Answer to:
Please use MATLAB to solve the question Computer Problem. Solve fi(x) = x – cos(x) =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT