Secant root finding method

Secant root finding method in Matlab script

Here, the expert is demonstrating the solution to two separate problems, one involves secant root finding method and the other is on binary expression.

  • In the first part he has given a complex equation for a hollow cylinder relating the length, radius, and height of the liquid with volume of the liquid contained in the cylinder. The equation involves use of inverse trigonometric function. The expert has shown to find the value of radius of cylinder with other parameters given using secant root finding method.
  • In the second part the expert has demonstrated writing of a Matlab script to convert a given binary expression into decimal system within given constraints.

SOLUTION

Solution Q. 1

clc;

close all;

clear all;

%General data

h=0.6;

L=5;

V=8;

%the function

f = inline(‘(r^2*acos((r-0.6)/r)-(r-0.6)*sqrt(2*r*0.6-0.6^2))*5-8’)

V = inline(‘(r^2*acos((r-0.6)/r)-(r-0.6)*sqrt(2*r*0.6-0.6^2))*5’)

%plot f vs r to see how the function behaves

rt=0.5:0.1:10;

for j=1:length(rt)

y(j)=V(rt(j));  %the Volume as a function of r

w(j)=f(rt(j));  %the volume -8 as a function of r

end

rn1=input(‘please enter the first approximation to r’)

rn2=input(‘please enter the second approximation to r’)

epsilon=input(‘please enter the relative error accepted’)

rn = (rn2*f(rn1) – rn1*f(rn2))/(f(rn1) – f(rn2)); %first approximation based on the 2 roots

flag = 1;

maxiter=100;

while abs(f(rn)) > epsilon

rn2 = rn1;

rn1 = rn;

rn = (rn2*f(rn1) – rn1*f(rn2))/(f(rn1) – f(rn2)); % The secant formula

flag = flag + 1;

if(flag == maxiter)

break;

end

end

if flag < maxiter

display([‘Volume is equal to 8 when r = ‘ num2str(rn)]);

else

display(‘Root does not exist’);

end

figure(1)

subplot(2,2,1:2)

plot(rt,y)

xlabel(‘radius’)

ylabel(‘Volume’)

grid on

hold on

scatter(rn,V(rn),’or’)

subplot(2,2,3:4)

plot(rt,w);

xlabel(‘radius’)

ylabel(‘Volume-8′)

grid on

hold on

scatter(rn,f(rn),’or’)

print(‘Output’,’-dpng’)

Solution Q 2.

clear all

clc

flag=1;

while flag==1

bin_num=input(‘enter a binary number as a string’)

bin_num=bin_num-‘0’;

dec=0;

PNT=0;

% Finding Decimal part

for i1=1:length(bin_num)

if(bin_num(i1)<0)

PNT=i1;

break;

end

dec(i1)=bin_num(i1);

end

D_DEC=sum(dec.*(2.^(length(dec)-1:-1:0)));

% Finding Fractional part

D_FRACT=0;

fract=zeros(1,length(bin_num)-PNT);

if (PNT>0)

for i2=PNT+1:length(bin_num)

fract(i2-PNT)=bin_num(i2);

end

D_FRACT=sum(fract.*(2.^-(1:length(fract))));

end

% Retrun Decimal equivalent

DEC_VAL=D_DEC+D_FRACT;

disp([‘The decimal value corresponding to the given binary number is = ‘ num2str(DEC_VAL)])

flag=input(‘do you want to convert another number? If yes please enter 1, otherwise enter 0’)

end