Matlab Programming Examples give you a brief overview of Matlab programs. Generally, Matlab programming can be done using object-oriented programming, GUI programming, and basic Matlab syntax and functions. Students must be aware of Matlab programming in order to implement a complete project in Matlab. We are also working with Matlab for the past ten years. We have worked on every version of Matlab, including the latest version (R2016a). Taking a project in Matlab is the best way to learn Matlab programming. We can offer matlab programming examples for you from thousands of projects as we have developed nearly 5000+ projects in Matlab. If you need any programs or projects in Matlab, mail us your requirements, we will give you your required code.

**Matlab Programming Examples**

**Matlab Programming Examples**

Our Matlab Programming Examples gives a brief knowledge about Matlab programming basics. To learn about Matlab programming, the basics of Matlab are essential. We have enumerated below, few example programs on functions, graphics, and arithmetic functions. These all are basic Matlab concepts that one has to know before taking complex programs. We also support advanced and complex programs in Matlab for students who aid in our help. We have a solution for every problem.

**Matlab Functions**

**Matlab Functions**

In general, Matlab functions are used to create all major applications in Matlab. All the functions are written in a file and saved as “filename.m.” All Matlab programs are written using m-files having an extension of “.m.” M-files are of two types, namely script files and script function files. New functions can also be created by merging existing functions. Function files have both input and output arguments. In short, it behaves like FORTRAN subroutines or C functions.

**Basic Syntax**

**Basic Syntax**

`Function outputs =my_fun(inputs)`

Code

..

..

..

Code

Outputs=….

**Sample Program For Functions**

**Sample Program For Functions**

Function y= my_fun(x)

y=x.sin(3*x.^2).*exp(-x.^2/4)

plot(x,y)

**Square Matrix Construction Using Functions**

**Square Matrix Construction Using Functions**

function B=inv_sq(A)

if abs(det(A)) <0.0001,

error(‘The matrix is singular’)

else

B=inv(A^2);

End

**Mathematical Function Example**

**Mathematical Function Example**

**To Solve also the equation ( x=cos x)**

% Sample program for functional iteration for script m-files:

% Anything following a percent sign is a comment

X=zeros (2, 1); % Approximate solutions stored in vector x

% Initially it is set to be a 2-by-1 vector of zeros.

X(1) =xguess % Initial guess goes in X(1)

for n=1: itmax %beginning of loop

x(n+1) = .5*cos(x(n));

diff = abs(x(n+1) –X(n)); % using absolute error for simplicity

if diff<tol

break % jump out of loop

end %end if clause

end % end of loop

if n < itmax %beginning of if clause

disp(“Iterations failed to coverage to prescribed tolerance.’).

end % end of if clause

x % displays the vector x

% end of sample program

**Advanced Graphics In Matlab**

*Using Matlab, we can develop different types of graphs like 2D curves, 3D surfaces, 2D and also 3D parametric curves etc. Major graphical operations are:*

- 3D plots
- Parametric plots
- Merging several graphs in single window.
- And so on

**Sample Program For Plotting**

**Sample Program For Plotting**

*To plot the graph a=b*^{2} in the interval [-1, 1]

*To plot the graph a=b*

^{2}in the interval [-1, 1]`>> b = -1:0.1:1`

>> a=b.*b

>> plot(a,b)

Note: To add more features to the above plot, use the following commands

>> title(‘Graph of a=b^2’)

>> blabel(‘b’)

>> alabel(‘a’)

**Arithmetic Operations In Matlab**

**Arithmetic Operations In Matlab**

In Matlab, Arithmetic operations can also performed on matrices and images. It includes addition, subtraction and also multiplications. In addition to it, it works on standard operations, vectorized functions and also operators, matrix equations using matrix division etc. Let’s take an Example of Image Arithmetic also to understand about Matlab arithmetic operations.

**Image Arithmetic In MATLAB With Example**

**Image Arithmetic In MATLAB With Example**

Generally, an image is represented in the form of Matrix. To perform Image arithmetic between two images, the size of two images must be same and also it result in a new image. Let the two images be X and also Y with same size:

**Input image:** RGB image

Z=X+Y (minimum value of X+Y is 255) //*Image Addition*

i.e. Z(a,b,1)=min(X(a,b,1)+Y(a,b,1),255) //*where (a,b) represents the pixel position.*

Z=X-Y (maximum value of X-Y is 0) //*Image Subtraction*

i.e. Z(a,b,:)=max(X(a,b,:)+Y(a,b,:),0)

Z=X.*Y; // *Image multiplication*

Z=X.\Y; //*Image division*

**Logical Operations On Image**

**Logical Operations On Image**

It is performed on pixel by pixel basis. Logical AND and also OR operations are used to select a sub image from an image. This masking process is known as ROI (Region of Interest processing). Now, consider a mask image ‘L’ for a image ‘X’. To obtain the acquired area using AND operation, use the following command:

**V= and (L, X); (or) V= L&X; **

**V=or (L, X); (or) V=L|X; ** //For OR operation

**Programming Example**

**Programming Example**

background=imread(‘back_ground.jpg’);

X=imread(‘ball.bmp’);

Y=imread(‘balloon.bmp’);

object=X+Y; %Image addition: Both X and Y are of same size

background=imresize(background,[size(object,1) size(object,2)]);

Im3=uint8(zeros(size(object)));

whiteImg=uint8(ones(size(object))); %Array right division. X./Y is the matrix with elements X(i,j)/Y(i,j). X and also Y must have the same size,

unless one of them is a scalar.

mask=whiteImg./object; %Image Division

%Logical AND operation

im3=uint8(mask&background);%uint8(and(mask,background));

figure,imshow(mask);

%Array multiplication. X.*Y is the element-by-element product of the arrays X and Y.

%Image multiplication

%Multiply the background and also the mask image

%And the result with the foreground image to obtain the final Image.

finalImg=(background.*im3)+object;

figure,imshow(finalImg);