Write a function function [xroot, froot] = newtonroot (func, x, h) that takes a
ID: 3795842 • Letter: W
Question
Write a function function [xroot, froot] = newtonroot (func, x, h) that takes a function func and returns he approximate root xroot as well as the function value froot at the approximate root using the formula shown below. Newton-Raphson root formula x_root x f(x_/f(X) In your function, call the function function from the previous homework, Problem 1, to compute the function derivative f' (x). Test your function with the following input (define w(t), y(x) as anonymous functions in the workspace): Write a function function [xroot, froot] = newtonroot_anon (func, x, h) to solve Problem 1 The only change from Problem 1 is that your function should use the ANONYMOUS function from the previous homework, Problem 2, to compute the function derivative f (x) NSTEAD of using the function file function). Test your function for the three cases from above.Explanation / Answer
Please make sure your funcderiv function is present in your directory.
%% program strats here
function [xroot, froot]=newtonroot(func,x,h)
dfbydx=funcderiv(func);
xroot=x-func(x)/dfbydx(x);
froot=func(xroot);
maxerr=10^-4; flag = 1; maxiter = 100;
while abs(froot) > maxerr
xroot=x-func(x)/dfbydx(x);
x=xroot;
flag = flag + 1;
if(flag == maxiter)
break;
end
end
xroot = x;
froot=func(xroot);
%display(['Root is x = ' num2str(c)]);
%% MATLAB Code for sacant method
clc;
close all;
clear all;
disp('Solution for a :')
y=@(x) 3*x;
[xroot, froot]= newtonroot(y,5,0.1)
disp('Solution for b :')
y=@(x) 3*x;
[xroot, froot]= newtonroot(y,5,10^-8)
disp('Solution for c :')
w=@(t) sin(t);
[xroot, froot]= secantroot(w,3,10^-4)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.