Professional Documents
Culture Documents
Mat Lab Only
Mat Lab Only
INTRODUCTION TO MATLAB
AIM
To understand MATLAB basic features and built in functions available in MATLAB.
EQUIPMENT AND SOFTWARE:
Personal Computer loaded with MATLAB 7.1
OPERATING SYSTEM:
Windows 7
THEORY:
MATLAB (matrix laboratory) is a fourth-generation high-level programming language
and interactive environment for numerical computation, visualization and programming.
MATLAB is developed by MathWorks.
It allows matrix manipulations; plotting of functions and data; implementation of
algorithms; creation of user interfaces; interfacing with programs written in other languages,
including C, C++, Java, and Fortran; analyze data; develop algorithms; and create models and
applications.It has numerous built-in commands and math functions that help you in
mathematical calculations, generating plots and performing numerical methods.
Local Environment Setup
If you are still willing to set up your environment, let me tell you a secret, setting up
MATLAB environment is a matter of few clicks. However, you need to download the installer
from here: MathWorks provides the licensed product, a trial version and a student version as
well. You need to log into the site and wait a little for their approval.
Once you get the download link, as I said, it is a matter of few clicks:
MATLAB : Help
Command Window
To execute commands in MATLAB environment.
Figure Window
To display graphical output from MATLAB code.
Workspace Window
To view variable definitions and variable memory allocations
Features of MATLAB
Following are the basic features of MATLAB:
It is a high-level language for numerical computation, visualization and application
development.
It also provides an interactive environment for iterative exploration, design and problem
solving.
It provides vast library of mathematical functions for linear algebra, statistics, Fourier
analysis, filtering, optimization, numerical integration and solving ordinary differential
equations.
It provides built-in graphics for visualizing data and tools for creating custom plots.
MATLAB's programming interface gives development tools for improving code quality
and maintainability and maximizing performance.
It provides tools for building applications with custom graphical interfaces.
It provides functions for integrating MATLAB based algorithms with external applications
Uses of MATLAB
MATLAB is widely used as a computational tool in science and engineering encompassing the
fields of physics, chemistry, math and all engineering streams. It is used in a range of
applications including:
Signal Processing and Communications
Image and Video Processing
Control Systems
Test and Measurement
Computational Finance
Computational Biology
RESULT:
AIM-
OPERATING SYSTEM:
Windows 7
THEORY
Variable names consist of a letter followed by any number of letters, digits or underscore.
MATLAB is case-sensitive. Variable names can be of any length.
Basic Commands
Save command : The save command is used for saving all the variables in the workspace, as a
file with .mat extension, in the current directory.
load command: You can reload the file anytime later using the load command.
Multiple Assignments : You can have multiple assignments on the same line.
For example, a = 2; b = 7; c = a * b;
Long Assignments : Long assignments can be extended to another line by using an ellipses (...).
acceleration = 9.8;
time = 20;
+ acceleration * time
The format function has the following forms used for numeric display:
Format Function Display up to
format short Four decimal digits (default).
format long 16 decimal digits.
format short e Five digits plus exponent.
format long e 16 digits plus exponents.
format bank Two decimal digits.
format + Positive, negative, or zero.
format rat Rational approximation.
Plotting Commands
MATLAB provides numerous commands for plotting graphs. The following table shows some of
the commonly used commands for plotting:
Command Purpose
axis Sets axis limits.
grid Displays gridlines.
plot Generates xy plot.
print Prints plot or saves plot to a file.
title Puts text at top of plot.
xlabel Adds text label to x-axis.
ylabel Adds text label to y-axis.
axes Creates axes objects.
close Closes the current plot.
close all Closes all plots.
figure Opens a new figure window.
subplot Creates plots in sub windows.
text Places string in figure.
bar Creates bar chart.
loglog Creates log-log plot.
polar Creates polar plot.
Creates semilog plot. (logarithmic
semilogx
abscissa).
Creates semilog plot. (logarithmic
semilogy
ordinate).
stairs Creates stairs plot.
stem Creates stem plot.
RESULT
MATRIX IN MATLAB
AIM:
Program:
A=
124
536
1 5 0 %matrix A output
B=
529
486
1 3 8 %Matrix B output
(a)ADDITION
Result:
add =
6 4 13
9 11 12
288
RESULT
All the matrix related operation has been performed.
EX NO 04 DATE
AIM:-
OPERATING SYSTEM:
Windows 7
THEORY:
1. Define x, by specifying the range of values for the variable x, for which the function is to
be plotted
Following example would demonstrate the concept. Let us plot the simple function y = x for the
range of values for x from 0 to 100, with an increment of 5.
x = [0:5:100];
y = x;
plot(x, y)
When you run the file, MATLAB displays the following plot
Let us take one more example to plot the function y = x2. In this example, we will draw two
graphs with the same function, but in second time, we will reduce the value of increment. Please
note that as we decrease the increment, the graph becomes smoother.
x = [1 2 3 4 5 6 7 8 9 10];
x = [-100:20:100];
y = x.^2;
plot(x, y)
When you run the file, MATLAB displays the following plot
Drawing Bar Charts
The bar command draws a two dimensional bar chart. Let us take up an example to demonstrate
the idea.
Example Let us have an imaginary classroom with 10 students. We know the percent of marks
obtained by these students are 75, 58, 90, 87, 50, 85, 92, 75, 60 and 95. We will draw the bar
chart for this data.
x = [1:10];
y = [75, 58, 90, 87, 50, 85, 92, 75, 60, 95];
bar(x,y), xlabel('Student'),ylabel('Score'),
title('First Sem:')
When you run the file, MATLAB displays the following bar chart
[x,y] = meshgrid(-2:.2:2);
g = x .* exp(-x.^2 - y.^2);
surf(x, y, g)
When you run the file, MATLAB displays the following 3-D map:
RESULT:
AIM-
OPERATING SYSTEM:
Windows 7
m=1;
zeta=0.1:0.1:1;
k=1;
wn=sqrt(k/m);
w=logspace(-1,1,400);
rad2deg=180/pi;
s=j*w;
for cnt=1:length(zeta)
xfer(cnt,:)=(1/m)./(s.^2+2*zeta(cnt)*wn*s+wn^2);
mag(cnt,:)=abs(xfer(cnt,:));
phs(cnt,:)=angle(xfer(cnt,:))*rad2deg;
end
for cnt=1:length(zeta)
figure(1)
loglog(w,mag(cnt,:),'k-')
title('SDOF frequency response magnitudes for zeta=0.2to1.0in steps of0.2')
xlabel('Frequency(rad/sec)')
ylabel('Magnitude')
grid
hold on
end
hold off
for cnt=1:length(zeta)
figure(2)
semilogx(w,phs(cnt,:),'k-')
title('SDOF frequency response phases for zeta=0.2to1.0 in steps of0.2')
xlabel('Frequency(rad/sec)')
ylabel('Phase')
grid
hold on
end
hold off
1
SDOF frequency response magnitudes for zeta=0.2to1.0in steps of0.2
10
0
10
M a g n it u d e
-1
10
-2
10
-3
10 -1 0 1
10 10 10
Frequency(rad/sec)
-20
-40
-60
-80
P has e
-100
-120
-140
-160
-180 -1 0 1
10 10 10
Frequency(rad/sec)
RESULT
MATLAB vibration problem were practiced thoroughly.
EX NO 06 DATE
AIM-
OPERATING SYSTEM:
Windows 7
Build Model
To model the four-bar linkage:
1. Start a new model.
2. Drag these blocks to the model. The Rigid Transform blocks specify the distance between
the two pivot mounts. This distance is the length of the implicit ground link.
Library Block Quantity
Pivot Mount 2
Binary Link A 2
Binary Link B 1
7. Connect and name the blocks as shown in the figure. You must position the frame ports of
the custom rigid body blocks exactly as shown.
Specify Block Parameters
1. In the Rigid Transform block dialog boxes, specify the offset between the pivot mounts
and the world frame.
Parameter Crank-Base Transform Rocker-Base Transform
2. In each binary link block dialog box, specify the length parameter.
Block Length (cm)
Binary Link A 10
Binary Link B 35
Binary Link A1 20
You can guide assembly so that the four-bar linkage assembles in an open configuration instead.
To do this, you must specify a position state target for at least one more joint. You do not have to
specify this target precisely. If you have a general idea of what the target should be, you can enter
an approximate value and select a low priority level for that target.
Closed-loop kinematic chains like the four-bar linkage are especially vulnerable to assembly
issues. Even when the model assembles, Simscape Multibody may fail to meet one or more state
targets. You can check the assembly status of the model and of the joints using the Model Report
utility:
1. In the Mechanics Explorer menu bar, select Tools > Model Report.
2. Examine the model report for red squares or yellow triangles. These shapes identify
issues in the assembly or in the joint state targets.
The figure shows the model report for the four bar linkage in the open configuration. A green
circle indicates that Simscape Multibody satisfied the Base-Crank Revolute Joint state target
precisely. A yellow circle indicates that Simscape Multibody satisfied the Base-Rocker Revolute
Joint state target approximately.
RESULT