Archive for category MATLAB

Interpolation, using MATLAB


Engineering problems often required the analysis of data pairs. For example, the data pair might represent cause and effect, or input-output relationship. In some applications, we want to estimate the variable’s value between the data points. This is called Interpolation. We will be discussing two types of Interpolation ie Linear Interpolation and Cubic-Spline Interpolation. We might get different estimation of values with both type of interpolation. It is impossible to say which estimation gives more accurate answer without studying the dynamics of the physical system. We must always keep in mind that our results will be approximate and should be used with caution.
Suppose a car is travelling along a straight line. The data from observations are given in the following table. We are asked to predict the speed of the car at t=15 min and t = 23

Time (min) 0 6 10 13 17 20 28 32
Velocity(km/hr) 0 6.67 17.33 42.67 37.33 30.10 29.31 28.74

Linear Interpolation:

Linear interpolation is so named because it is equivalent to connecting the data points with a straight line.
Linear interpolation in MATLAB is obtained with the interp1 and interp2 functions.interp1 is used to linearly interpolate a function of one variable only: y =f(x)
Where as interp2 is used to linearly interpolate a function of two variables: z = f(x,y)
The syntax of interp1 is rather simple.
interp1(x,y,est_x)
where,
x is a vector containing independent variable data;
y is a vector containing dependent variable data;
est_x is vector containing the value(s) of independent variable at which we want to interpolates.
Lets see how does m-file looks like:
m-file will be something like this

clc
clear
x = [0 6 10 13 17 20 28 32];
y = [0 6.67 17.33 42.67 37.33 30.10 29.31 28.74];
x_est = [15 23];
y_est = interp1(x,y,x_est)


Cubic-Spline Interpolation:

In Spline interpolation we get smooth curve of the function through a set of points rather than sharp edges at data points.
The syntax of Spline interpolation in MATLAB is very similar to linear interpolation. We just have to replace ‘interp1’ with ‘spline’
Therefore,
spline(x,y,est_x)
where,
x is a vector containing independent variable data;
y is a vector containing dependent variable data;
est_x is vector containing the value(s) of independent variable at which we want to interpolates.

clc
clear
x = [0 6 10 13 17 20 28 32];
y = [0 6.67 17.33 42.67 37.33 30.10 29.31 28.74];
x_est = [15 23];
y_est = spline(x,y,x_est)

The plots of both the interpolation graphs are shown below. The dashed lines represents linear interpolation, and the solid curve is the cubic spline.
interpolation, matlab, spline, cubic, numerical, analysis

Linear and Spline interpolation graph

For more stuff on Numerical Analysis using MATLAB,

View Hammad Ansari's profile on LinkedIn

, , , ,

3 Comments

Solving System of Equations using MATLAB


This is a continuation of my previous post, in which we learned how to find out roots of any equation using MATLAB. In this post, we will try to solve a system of equations. In Engineering, we frequently encounter with such problems in which we have to solve system of equations.

One of the easiest ways to solve system of equations is ‘The Matrix Inverse Method’.

Suppose we are given the following equations.


We can write these equations in the form of matrix:


or


Using the same idea, we can solve system with any number of equations.

Let’s take a practical example related to electrical engineering.

Suppose you are given the following circuit and you are asked to calculate the current through all the resistance.

Our first step is to find out the equations using KCL and KCL. I assume that you know how to use KCL and KCL to find the equations. Using KCL and KCL, we have


Solving by ‘Inverse Matrix Method’



I am sure you don’t want to solve this manually if you have a choice of using MATLAB. It may be difficult for you but not at all for MATLAB.

MATLAB has built-in function to calculate the inverse of a matrix. Using this function, we can easily calculate the required unknowns buy simple matrix multiplication of Inverse of A and b.

Here a code for you guys:


clc
clear
A=[25,0,2,-10,0,0;0,10,0,10,0,2;0,0,2,0,100,2;1,0,-1,0,1,0;-1,1,0,-1,0,0;0,1,0,0,1,-1]
b=[130;460;590;0;0;0]
x=inv(A)*b

, , ,

3 Comments

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

, , , , ,

2 Comments