Sometimes, we come across mathematical problems that are very difficult and tedious to solve manually. For that, we have to use some dedicated software to solve the problem. This post is based on tutorials of solving some types of mathematical problems using MATLAB.

Numerical problems can be divided into following types:

- Finding Roots of an equation
- Solving system of equations
- Interpolation
- Integration and Differentiation
- Solving Differential Equations

**Roots of an equation using MATLAB:**

Suppose we are trying to find root of an equation:

*f(x) = x – 3*

or

*f(x) = x^2 – 9*

Now these equations are quite easy to solve manually. You don’t really need MATLAB to solve these equations. But, what if you encounter with equations like

*f(x) = cos(x) – x*exp(x)*

There are many numerical methods to find roots of such equations. But here, I am going to present only two of them ie Bisection Method and Secant Method.

clc

clear

format long

A =@(x) cos(x)-x.*exp(x);

a = 1;

b = 0;

c = @(a,b) (a+b)./2;

NumOfIteration = 20;

for i =1:1:NumOfIteration

disp(‘********************************’)

disp(sprintf(‘Interation number %i’,i))

disp(‘********************************’)

Interval = [a b]

a_val=A(a);

b_val=A(b);

c_val=A(c(a,b));

if a_val*c_val<=0

b=c(a,b);

else if b_val*c_val<=0

a=c(a,b);

else

disp(‘Solution Doesnt Exist within Intervals’)

end

end

end

disp(‘For more tutorials, visit’)

disp(‘My blog‘)

Here is a code for Secant Method:

clc

clear

format long

A =@(x) cos(x)-x.*exp(x);

a = 1;

b = 0;

c =@(a,b)(a)- ( ((b-a).*A(a))/(A(b)-A(a)));

NumOfIteration = 10;

for i =1:1:NumOfIteration

disp(‘********************************’)

disp(sprintf(‘Interation number %i’,i))

disp(‘********************************’)

Interval = [a b]

a_val=A(a);

b_val=A(b);

c_val=A(c(a,b));

temp=c(a,b);

a=b;

b=temp;

end

disp(‘For more tutorials, visit’)

disp(‘My blog‘)

In MATLAB, it is a good practice to add *‘clc’* and *‘clear’* at the beginning of any program. Here,** A** is a function of

*x*whose roots is to find. We provide two initial guesses

**a**and

**.**

*b*Try solving the following questions using the same above MATLAB code:

- f(x) = exp(-x) – x
- f(x) = square_Root(x) – cos(x)

Note that Secant Method has much higher convergence rate.

Keep visiting my blog for more tutorials, coming soon!