Numerical Analysis using MATLAB


Numerical, Analysis, Roots, Equation, System, Integration, Differentiation, Differential, Interpolation

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:

  1. Finding Roots of an equation
  2. Solving system of equations
  3. Interpolation
  4. Integration and Differentiation
  5. 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:

  1. f(x) = exp(-x) – x
  2. 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!


View Hammad Ansari's profile on LinkedIn

Advertisements

, , , , ,

  1. Solving System of Equations using MATLAB « Hammad Ansari's Blog
  2. teeth whitening scottsdale

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: