Secant root finding method in Matlab script

Secant root finding method

Numerical analysis is one of the topics in computer science which involves the use of numerical approximation. This field has a lot of application in engineering and natural sciences. It’s one of the areaswhich finds huge application in real-life scenarios.  Students, therefore, should take this topic seriouslyand grasp a lot of academic contents. Assignments in numerical analysis can be challenging to most students. A student needs to be good at computing complex numerical problems if he/she is to succeed in this area. Assignments in this area could include evaluating integrals, approximation, interpolation, differential equations, and computing the values of a function. In some cases, students would study the root finding method. This is where this article will find its application.

Root finding method.

Root finding can be said to be the process of finding the zeros of an equation.  Let’s say we have a function f(x). We say that we have found the roots of an equation if we can solve for x at f(x) =0.  The obtained values are referred to as the roots of the equation. In other words, we can say that the root of an equation is that value that satisfies the equation.

Things might not be clear to you; let’s take an example of a quadratic equation having the formula Ax2 + Bx +C. If we can solve for x then be sure we shall have our roots. We begin by equating the general function to zero and solve for the values of x, and thus we get the roots of the equation.

There are different methods used to obtain the root of the equation. One could use the completing square method, a quadratic formula, or factoring. Whichever method is chosen, its best to note that a quadratic equation will always have two roots.

Different methods of finding the roots of an equation.

We have seen thatmethods such as completing square method, quadratic formula, and factoring can be applied to finding the root of a function. However, there are other methods that can be used to find the roots of a polynomial equation of any degree. Software can also be used to solve an equation using these methods. They are the secant method, bisection method, and newton’s method. We won’t dwell into explaining what each of them does but will jump straight into explaining the secant method. This is where we delve deeper from now.

The secant method.

It is an algorithm used to get the roots of function f(x) which is nonlinear. This type of algorithm is closely related to the newton method. It derives its name from a mathematical term known as a secant. A secant line is a line that divides a function or a curve into two distinct points. For a circle, it could intersect it into two equal parts. It’s widely used in geometry. However, this does not necessarily imply that we will be drawing lines here.

Mathematical interpretation.

Let’s take an arbitrary function f(x) and assume that it is continuous over the interval [a, b]. Also, we make an assumption that f (a).f (b) <0.  Then using the intermediate value theorem, we are guaranteed of a solution if we get x-values such that f(x) =0. Much does not change here. We still remain with the primary role of any root finding method-finding x-values.

The algorithm

It simply a root finding algorithm. But how does it work? You must be curious about its mechanism.  Here we adopt an approach more similar to the regulafalsi method.

The first step begins with an initial guess of the starting values a and b. These values have to satisfy the condition that f(a).f(b) <0. Otherwise, select new starting points.

Secondly, we take the values as the intervals and find the next value which is c using the formula

 

The third step is to find the solution of the equation by substituting c to the function. If it affirms the equation f(c) = 0, we have found the root—otherwise, a=b and b=c.

Then we have to repeat the process from step two until we can get the root of the equation.

It should be noted that the process is an iterative one and involves guessing a lot. Getting to the root of the equation without a lot of hassle is largely dependent on the initial value guess. With software such as Matlab, this can be done in a much easier way.

Implementation of the secant algorithm in Matlab.

Matlab is a mathematical software capable of doing large and complex mathematical computations faster with relative ease. Also, making a Matlab code is easier as compared to other higher-level programming languages in its category. Debugging is a lot easier. You will not have to waste a lot of time to make sure that the code works. Because of its advantages, the science community prefers it to other programming languages.

Finding the secant root of a function, will require you to develop your own function. You will need to use the loops and other in-built functions such as the tanh to find the roots of the equation. You won’t find it very challenging. But in case you cannot make your own code, you could do with a little help from the internet. There are lots of code that can help you.

The secant method vs. the newton equation.

The newton method is an application that is easily implemented in many statistical software such as python and matlab. However, this method involves the user to guess the values of x.  With the initial values, the user can increment the x-values with a constant until he/she finds the values that converge.

When comparing the two methods of finding the root of an equation, we take into consideration two main factors speed and cost. Which one takes a shorter time to evaluate? Well, one can easily assume that newton’s method is faster because of the fact that it converges quickly. However, this is a wrong notion. An algorithm that converges quickly might take a long time overall than a slower one.

When comparing costs, one method that is used is the number of functions per evaluation. Generally, the newton’s methods will take two functions per evaluation, while the secant method only takes one function.

Advantages of the secant method.

It converges faster than any other root finding method and is therefore considered to be a faster method of finding the roots of an equation.

It does not require you to have knowledge of derivatives.

Disadvantages

It has problems with convergence when f (xn) =f (xn+1) and sometimes when the x-axis is tangential to the curve.

Matlab assignment experts

We are an online homework assistance company that has helped a lot of students like you in Matlab related assignments.  For the years that we have been in service, we have always provided our esteemed clients with top-notch service. Our goal has always been to get you the grade that you always desire but seems elusive to you otherwise. With the experienced and highly trained experts that we possess in our arsenal, we are confident that we will help you in procuring a high-quality grade. If you doubt us check the numerous positive reviews that we have received from the clients we have served before.

Contact us if you want help with secant root finding method homework help. We will help you score a top grade at an affordable price.  You can also contact us for assignment and project help.  We will be more than glad to lend a helping hand.

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