Write a Matlab function that takes as input x and outputs f(x), when given a roo
ID: 3109941 • Letter: W
Question
Write a Matlab function that takes as input x and outputs f(x), when given a root finding problem f(x) = 0. Then write a Matlab function that takes as input: a, b, endpoints of starting interval [a, b]; number of iterations N; and outputs the method of false position's approximation p_N (note p_0 = a, p_1 = b). Make sure you call the first function when you need values of f. (a) Write out or print out your programs for the case f(x) = cos x - x. (b) Write down your results when the starting interval endpoints are a = 0, b = pi/2 and N = 1, 5, 10, 20.Explanation / Answer
clc;
clear all;
f=@(x) cos(x)-x ; %Given function
a=input('Starting point a=');
b=input('End point b=');
N=input('Number of iteration N=');
% a=0; %starting point
% b=pi/2; % end point
if(f(b)<f(a))
m=a;
a=b;
b=m;
end
c=1;
while((abs(a-b)>1e-2)&&c~=N)
c=c+1;
m=(a*f(b)-b*f(a))/(f(b)-f(a))% false method formula
if(f(m)>0)
b=m;
end
if(f(m)<0)
a=m;
end
err(c)=abs(a-b);
end
%% result for N=5
Starting point a=0
End point b=pi/2
Number of iteration N=5
m =
0.6110
m =
0.7233
m =
0.7373
m =
0.7389
% Result for N=10
m =
0.7391
% Result for N=15
m =
0.7391
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.