Hi friends, in this MATLAB tutorial we are going to calculate Maclaurin Series of order n. We know that A Maclaurin series is a Taylor series expansion of a function about 0,

Let’s see a MATLAB program to calculate Maclaurin Series of order n.

MATLAB Program for Maclaurin Series

Initially, it will take input x as theta of cos. Then it will ask how many terms the series should expand. By using for loop and Maclurin Series formula it will generate the series up to the terms required.

%Maclurine series%
x = input('enter value of cosx :');
n=input('enter last value:');
for i=1:n
series=(((-1)^n)/(factorial(2*n)))*(x^(2*n));
series=series+series;
end
series

Download: You can download this MATLAB program here.

This article is written by Gohar Jamal. I hope you liked this article. Like our facebook page and subscribe to our newsletter for future updates. If you have any queries feel free to ask in the comment section below. Have a nice day!

Representation of basic discrete time signal using MATLAB:

Hi friends, today we are going to discuss about discrete time signals and how to plot graphs of different discrete time signals such as step signal, ramp signal, impulse function, exponential, sine and cosine signals using MATLAB.

Before going towards actual programming part, let us recall the definition of discrete time signal.

“In discrete time signal the value of signal is specified only at specific time. So signal represented at “discrete interval of time” is called as discrete time signal.”

MATLAB Program:

clc;

clear all;

close all;

x=[0:15];

for i=1:16

y(i)=1;

end

subplot(2,3,1)

stem(x,y)

title(‘Discrete time Step Function’)

xlabel(‘Samples’)

ylabel(‘Step function’)

for i=1:16

y(i)=x(i);

end

subplot(2,3,2)

stem(x,y)

title(‘Discrete time Ramp Function’)

xlabel(‘Samples’)

ylabel(‘Ramp signal’)

y=[zeros(1,4),1,zeros(1,11)];

subplot(2,3,3)

stem(x,y)

title(‘Discrete time Impulse Function’)

xlabel(‘Samples’)

ylabel(‘Impulse function’)

for i=1:16

y(i)=exp(-0.22*i);

end

subplot(2,3,4)

stem(x,y)

title(‘Discrete time Exponential Function’)

xlabel(‘Samples’)

ylabel(‘Exponential function’)

z=[1:35]

for i=1:35

y(i)=sin(2*pi*0.1*i)

end

subplot(2,3,5)

stem(z,y)

title(‘Discrete time Sine Function’)

xlabel(‘Samples’)

ylabel(‘Sine function’)

z=[1:35]

for i=1:35

y(i)=cos(2*pi*0.1*i)

end

subplot(2,3,6)

stem(z,y)

title(‘Discrete time Cosine Function’)

xlabel(‘Samples’)

ylabel(‘Cosine function’)

Explanation:

Representation of discrete time step function:

x=[0:15];

for i=1:16

y(i)=1;

end

subplot(2,3,1)

stem(x,y)

title(‘Discrete time Step Function’)

xlabel(‘Samples’)

ylabel(‘Step function’)

For representing discrete time step function, let us consider x-axis represents number of samples and y-axis represents corresponding values of signal. Here we use two matrix x and y. x=[0:15] represents matrix ‘x’ contains 16 elements from 0 to 15. Then we used one for loop from 1:16 (16 times) in which we define matrix y, which stores ‘1’ entire the matrix. Then we give command subplot(2,3,1). Here we used subplot command because we want all the basic signals on a single display. (2,3,1) represents there are 2 rows and 3 columns of graphs (it means 2*3=6 graphs are possible) 1 indicates that we want this graph at first position.

As we know for continuous signal plotting we use plot command but for discrete time signal we have to use stem command.

Similarly for discrete time ramp signal we use y(i)=x(i) keeping all the program same.

For impulse function we use y=[zeros(1,4),1,zeros(1,11)] that means first 4 elements of the matrix are ‘0(zeros)’ 5^{th} element is ‘1’ and from 6^{th} to 16^{th} it is again ‘0’. Thus y becomes 16 element matrix. Thus we can plot graph of impulse function using matlab.

For exponential signal we have used exp(-0.22*i). Here negative sign indicates that the nature of exponential signal is decreasing. But if you remove the negative sign it will become simple increasing exponential signal.

Similarly we can plot discrete time sine and cosine functions using MATLAB

The preceding methods of solving simultaneous linear equations are known as direct methods as they yield exact solution. On the other hand an iterative method is that in which we starts from an approximation to the true solution and obtain better and better approximation from a computation cycle repeated as often as may be necessary for achieving desired accuracy. Simple iteration methods can be devised for systems in which the coefficient of leading diagonal are large compared to others.

There are two iterative methods as follows:

1) Jacobi’s iteration method.

2) Gauss Seidel iteration method.

Today we are just concentrating on first method that is Jacobi’s iteration method. We will see second method (Gauss Seidel iteration method) for solving simultaneous equations in next post. After that i will show you how to write a MATLAB program for solving roots of simultaneous equations using Jacobi’s Iterative method.

Jacobi’s iteration method:

Let us consider set of simultaneous equations as follows:

a1x+b1y+c1z=d1…………………1)

a2x+b2y+c2z=d2…………………1)

a3x+b3y+c3z=d3…………………1)

If a1, b2, c3 are large as compare to other coefficient then solving these for x,y, and z respectively. Then the system can be written in the form:

x = (k1) – (l1)y – (m1)z…………..2)

y = (k2) – (l2)x – (m2)z…………….2)

z = (k3) – (l3)x – (m3)y…………….2)

Let us starts with the initial approximations x0=k1, y0=k2 and z0=k3.(by putting x=y=z=0 in right hand side above equation)

Then the second approximation is given by,

x1 = (k1) – (l1)y0 – (m1)z0…………..3)

y1= (k2) – (l2)x0 – (m2)z0…………….3)

z1 = (k3) – (l3)x0 – (m3)y0…………….3)

Third approximation is given by,

x2 = (k1) – (l1)y1 – (m1)z1…………..4)

y2= (k2) – (l2)x1 – (m2)z1…………….4)

z2 = (k3) – (l3)x1 – (m3)y1…………….4)

This process is repeated till difference between two consecutive approximations is negligible.

Let us understand this method in detail by solving one simple example.

Example:

Solve following set of simultaneous equations using Jacobi’s iterative method.

20x+y-2z=17

3x+20y-z=-18

2x-3y+20z=25

Solution:

We write the given equations in the following form:

x = (1/20)(17-y+2z)……………..1)

y = (1/20)(-18-3x+z)…………….1)

z = (1/20)(25-2x+3y)……………1)

Substitute x0=y0=z0=0 in above equation we get,

x1 = (1/20)(17); y1 = (1/20)(-18); z1=(1/20)(25).

Or x1=0.85; y1=-0.90; z1=1.25

Put x1, y1 and z1 in equation (1)

x2 = 1.02; y2 = -0.965; z2 = 1.03.

Put x2, y2 and z2 in equation (1)

x3 = 1.001; y3 = -1.001; z3 = 1.003.

Put x3, y3 and z3 in equation (1)

x4 = 1.0004; y4 = -1; z4=0.99975

Put x4, y4 and z4 in equation (1)

x5 = 1; y5 = -1; z5=1

Now it is sufficient. If you observe above two sets of roots, they are almost same.

Hence the roots of given simultaneous equations using Jacobi’s iterative method are:

Lagrange’s Interpolation Formula is used to determine the value of any function f(x), which is known at discrete points. That is if we have any function with its value at different points such as, at x=0, 1, 2… So using Lagrange’s Interpolation Formula, we can determine value of that function at any point.

Derivation:

We can derive the Lagrange’s Interpolation formula by using Newton’s divided difference formula. If f(x) is approximated with an Nth degree polynomial then the Nth divided difference of f(x) constant and (N+1)th divided difference is zero. That is

as we know Lagrange’s interpolation is Nth degree polynomial approximation to f(x) and the Nth degree polynomial passing through (N+1) points is unique hence the Lagrange’s and Newton’s divided difference approximations are one and the same. However, Lagrange’s interpolation formula is very useful for the computer programming while Newton’s difference formula is convenient for the hand calculations.

Question: Given set of values of x and y (5,12),(6,13),(9,14),(11,16) Find the value of x corresponding to y=15 using lagrange interpolation

Solution:

Tabular the given data:

y: 12 13 14 16

x: 5 6 9 11

Applying lagrange interpolation formula,

x(y)=(y-13)(y-14)(y-16)*5/(12-13)(12-14)(12-16)+

(y-12)(y-14)(y-16)*6/(13-12)(13-14)(13-16)+

(y-12)(y-13)(y-16)*9/(14-12)(14-13)(14-16)+

(y-12)(y-13)(y-14)*11/(16-12)(16-13)(16-14).

By putting y=15 we get x(15)=11.5

MATLAB code for lagrange interpolation formula:

%Created by myclassbook.org %Created on 26 May 2013 %lagrange interpolation formula

% Question: Given set of values of x and y (5,12),(6,13),(9,14),(11,16) % Find the value of x corresponding to y=15 using lagrange interpolation

clc; clear all; close all; y=[12 13 14 16]; %Change here for different function x=[5 6 9 11]; a=15;

Addition of two images using MATLAB image processing:

In this tutorial you will learn how to add two images. Yes it is possible in MATLAB. You can add i.e. superimpose two images. Following are the steps to add two images using MATLAB image processing:

The only requirement for superimposition (or addition) of two images is that, the two matrix that you have defined for two images should have same size (as we know MATLAB is totally works with matrices and the addition of two matrices is possible only when they are of same size).

Let us consider two images

1] H:New photorock 1.jpg

2] H:New photoImagesPhoto1561.jpg

(These are two images that I have chosen, but for your case it may be different. For copying the image path visit my previous post)

MATLAB code for addition of two images:

%Addition of two images

%Created by : myclassbook.org (mayuresh)

%Created on : 21 May 2013

A=imread(‘H:New photorock 1.jpg’); %define A as a first image

B=imread(‘H:New photoImagesPhoto1561.jpg’); %define B as a second image

C=imresize(A,[400 400]); % image resize of A as 400*400 matrix

D=imresize(B,[400 400]); % image resize of B as 400*400 matrix

E=imadd(C,D); % image addtion of C and D

imshow(E) % display of image E

OUTPUT:

tags : addition of two images in matlab. matlab overlay two images matlab display two images how to add two images in matlab. subtracting two images in matlab. matlab align two images how to add two images in matlab. add two strings matlab subtract images in matlab matlab subtract.

Color to grayscale conversion MATLAB image processing tutorial:

Hi friends, this is my first tutorial on Matlab image processing .The purpose of this tutorial is to gain familiarity with MATLAB’s Image Processing Toolbox. Let us start the basic of image processing with a very simple example i.e. converting a color image of any format (ex. jpeg,jpg,bmp,etc) into a grayscale image.

MATLAB program for color to grayscale image conversion:

Step 1:

Initially you have to define a matrix A with a path of the image that you have to convert. For this purpose if you cant copy a path of the image file, just install this small software (http://www.novell.com/coolsolutions/tools/downloads/pathc400.zip) after installing this software simply right click on image you will see an option for copy long url. OK after copying the image path.

Step 2:

Define matrix B with the image format. In this case jpg. You can change the format you have.

Step 3:

C=rgb2gray(B). Here rgb stands for RedGreenBlue i.e. color image gray stands for converting it into grayscale.

%tutorial M-file %created by : myclassbook.org %created on : 20 may 2013

% Load a color .jpg image and convert it into grayscale

A=’H:New photoImagesPhoto1619.jpg’; %designate matrix A as the spcified file B=imread(A,’jpg’) %matrix B loaded with jpg file specified by A figure (1),imshow(B); %show image B in figure window 1 C=rgb2gray(B) %convert color image to grayscale image figure (2),imshow(C); %show image C in figure window 2

Newton’s Forward Interpolation Formula with MATLAB Example :

Interpolation:

Introduction:

In everyday life, sometimes we may require to find some unknown value with the given set of observations. For example, the data available for the premium, payable for a policy of Rs.1000 at age x, is for every fifth year. Suppose, the data given is for the ages 30, 35, 40, 45, 50 and we are required to find the value of the premium at the age of 42 years, which is not directly given in the table. Here we use the method of estimating an unknown value within the range with the help of given set of observation which is known as interpolation.

Definition of interpolation:

Given the set of tabular values (x0, y0), (x1, y1),…,(xn, yn) satisfying the relation y=f(x) where the explicit nature of f(x) is not known, it is required to find a simpler function say ф(x), such that f(x) and ф(x) agree at the set of tabulated points. Such a process is called as interpolation.

If we know ‘n’ values of a function, we can get a polynomial of degree (n-1) whose graph passes through the corresponding points. Such a polynomial is used to estimate the values of the function at the values of x.

We will study two different interpolation formula based on finite differences, when the values of x are equally spaced. The first formula is:

Where ‘a+ph’ is the value for which the value of the function f(x) is to be estimated. Here ‘a’ is the initial value of x and ‘h’ is the interval of differencing.

Question:

Table gives the distance between nautical miles of the visible horizon for the given height in feet above the earth surface. Find the value of y when x= 218 feet .

MATLAB program for Newtons forward interpolation formula :