The following function has many roots. f(x)-2x0.1 + x05 sin(x)-0 a) Write a gene
ID: 3168017 • Letter: T
Question
The following function has many roots. f(x)-2x0.1 + x05 sin(x)-0 a) Write a generalized MATLAB function that uses the incremental search algorithm to bracket a root in a function. The function should search for a root starting at x0 with a step size of step and returns the bounding interval for the first root that is encountered. A suitable declaration of the function would be: tunction increnental (tune, xo,atep) Functiàn to implement the ineenental earch algo ithn § The argunents are : func -a function handle, xo - the atarting § value for the search, step- the step size for the search. Te function ahould return an interval bounding the root x - lbegin,end] ALCORIT end b) Write a MATLAB script that finds the first 20 roots (starting at x = 0 and searching in the positive direction with a step size of 05) of the nonlinear equation given above. The script must use the incremental search function developed in part (a) to bound the root and then use the fzero function to converge on cach root. Finally, the script should print the list of roots to the command prompt.Explanation / Answer
A)
%%% Matlab function %%%%%
function g = incremental( fun , x0 ,step );
f=fun;
temp=1;
a=x0;
while(temp)
f1=feval(f,a);
b=a+step;
f2=feval(f,b);
if( f1*f2 < 0)
temp=0;
break;
end
a=b;
end
g=[a,b];
end
B)
%%%%% Matlab code %%%%%%%%
clc;
clear all;
close all;
format short
%%% restoredefaultpath %%%
f=@(x) 2*x^0.1+x^0.5*sin(x);
x0=0;
step=0.5;
for n=1:20
g(n,:)=incremental(f,x0,step);
x0=g(n,2);
end
%%%% finding roots
for n=1:20
r=fzero(f,g(n,:));
fprintf('Roots in interavl [ %f,%f ] is : %f ',g(n,1),g(n,2),r);
end
OUTPUT:
Roots in interavl [ 10.000000,10.500000 ] is : 10.329041
Roots in interavl [ 11.500000,12.000000 ] is : 11.722573
Roots in interavl [ 16.000000,16.500000 ] is : 16.419449
Roots in interavl [ 18.000000,18.500000 ] is : 18.171855
Roots in interavl [ 22.500000,23.000000 ] is : 22.603257
Roots in interavl [ 24.500000,25.000000 ] is : 24.543202
Roots in interavl [ 28.500000,29.000000 ] is : 28.822775
Roots in interavl [ 30.500000,31.000000 ] is : 30.884052
Roots in interavl [ 35.000000,35.500000 ] is : 35.060526
Roots in interavl [ 37.000000,37.500000 ] is : 37.208995
Roots in interavl [ 41.000000,41.500000 ] is : 41.309097
Roots in interavl [ 43.500000,44.000000 ] is : 43.524340
Roots in interavl [ 47.500000,48.000000 ] is : 47.564723
Roots in interavl [ 49.500000,50.000000 ] is : 49.833343
Roots in interavl [ 53.500000,54.000000 ] is : 53.825260
Roots in interavl [ 56.000000,56.500000 ] is : 56.137886
Roots in interavl [ 60.000000,60.500000 ] is : 60.089380
Roots in interavl [ 62.000000,62.500000 ] is : 62.439147
Roots in interavl [ 66.000000,66.500000 ] is : 66.356213
Roots in interavl [ 68.500000,69.000000 ] is : 68.737904
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.