Hi, I’m trying to use the bisection method to find x. Think there’s a problem using the function. If you have any hints to help me fix it, it would be much appreciated. Thanks
clck=1007.564; %stiffness (N/m)
m=98.668; %mass (kg)
F_0=108.764; %applied force magnitude (N)
x_0=-0.02; %initial displacement (m)
v_0=0.1; %initial velocity (m/s)
t=4.54; %time (s)
x_1=0.0386; %displacement (m)
om_n=sqrt(k/m);f_0=F_0/m;x=0; %omega
function bisection n = 20; % initial interval
a = 5.12; b = 6.45; fprintf('\n initial interval [%g, %g] \n total bisection steps %d\n', a,b,n); %initialise and check that there is a root in the prescribed interval
x_left = a; x_right = b; f_left = f(x_left); f_right = f(x_right); if f_left*f_right > 0 error('ERROR: no root in the specified interval'); end %the bisection method
for i=1:n if f_left == 0 %exact root is reached by the left bound of the interval
fprintf('\n stage %g root %g with zero absolute error \n',i,x_left); return; end if f_right==0 %exact root is reached by the right bound of the interval
fprintf('\n stage %g root %g with zero absolute error \n',i,x_right); return end %the bisection process
x_mid = (x_left+x_right)/2.0; f_mid = f(x_mid); if f_left*f_mid <= 0 %there is a root in [x_left,x_mid]
x_right = x_mid; f_right = f_mid; end if f_left*f_mid > 0 % there is a root in [x_mid,x_right]
x_left = x_mid; f_left = f_mid; end %compute the approximate rood for the current bisection step
root = (x_left+x_right)/2.0; %compute the absolute error for the current bisection step
abs_err=(x_right-x_left)/2.0; fprintf('\n stage %g root %g absolute error < %g \n',i,root,abs_err); end %check satisfaction of equation at end of process
residual = f(root); fprintf('\n final residual = %g \n',residual);end%Subfunction defines the equation f(x) = 0
function f_value = f(x) %x=omega
f_value = (v_0/om_n)*sin(om_n*t) + (x_0-((f_0)/(om_n)^2-(x)^2))*cos(om_n*t) + ((f_0)/(om_n)^2-(x)^2)*cos(x*t);end
Best Answer