MATLAB % Finction I want to use Bisection Method for this problem as the followi
ID: 2902630 • Letter: M
Question
MATLAB
% Finction
I want to use Bisection Method for this problem as the following code:
function [ c,k ] = bisection( f,a,b,tol )
ya = f(a);
yb = f(b);
if (ya*yb > 0)
disp('you picked a bad interval')
return
end
max1= 1 + round(log((b-a)/tol)/log(2));
for k= 1:max1
c=(a+b)/2;
yc=f(c);
if yc==0
a=c;
b=c;
elseif (yc*yb<0)
a=c;
ya=yc;
else
b=c;
yb=yc;
end
if (b-a<tol)
break
end
end
%Script
f=@(x) cos(x)+(1./(x.^3+200));
a=-5;
b=-4;
tol=1e-10;
[c,k]=bisection(f,a,b,tol);
fprintf ('#of iteraction is %d, 1st root is %20.10e ',k,c)
There are 2 files, one for the function and the other one is script file. I want the answer which finding the required time to be as the last line in the script file
Taken from Applied Numerical Methods with MATLAB, Chapra, 3e, 2008. The upward velocity of a rocket is v = u In (m0/m0 ? qt) - gt where v is the upward velocity (m/s), u is the velocity at which fuel is expelled relative to the rocket (m/s), m0 is the initial mass of the rocket (kg), q is the fuel consumption rate (kg/s), and g = 9.812 is the gravitational constant (m/s^2). Use your bisection algorithm to determine the time t (s) it takes for the rocket to reach velocity v = 750 m/s when u = 1800 m/s, m0 = 160,000 kg with q = 2600 kg/s. High resolution is not needed here, so let tol = le - 4. In Project 2, you will generalize this problem by letting q vary to see how t is affected.Explanation / Answer
function [t]=bisection(N)
tol=1e-4;
m_0=160000;
q=2600;
a=0; % Left endpoint
b=m_0/q; % Right endpoint
for k=1:N
c=(a+b)/2;
if (f(c)==0)
t=c;
return;
elseif (f(c)*f(a)<0)
b=c;
else
a=c;
end
if (abs(f(a))<abs(f(b)) && abs(f(a))<tol)
t=a;
return;
elseif (abs(f(b))<tol)
t=b;
return;
end
end
disp('Not converges for this N!');
end
function y=f(t)
v=750;
g=9.812;
u=1800;
m_0=160000;
q=2600;
y=v-u*log(m_0/(m_0-q*t))-g*t;
end
---------------------------------------------------------Output------------------------------------------------
>> bisection(10)
Not converges for this N!
>> bisection(100)
ans =
17.0247
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.