# Archive for category MATLAB

### Interpolation, using MATLAB

Posted by Hammad_Ansari in MATLAB on June 20, 2011

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)*

For more stuff on Numerical Analysis using MATLAB,

Follow @Hammad_Ansari### Solving System of Equations using MATLAB

Posted by Hammad_Ansari in MATLAB on June 15, 2011

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:

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

Follow @Hammad_Ansari

### Numerical Analysis using MATLAB

Posted by Hammad_Ansari in MATLAB, Uncategorized on June 14, 2011

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!