Lab 03 MATLAB
Lab 03 MATLAB
Lab 03 MATLAB
Lab 03
Objective:
Introduction to MATLAB: generating graphical plotings, using curve fitting and dealing
with GUIs with certain applications. (Part-III)
Theory:
Graphical Ploting:
MATLAB is a powerful tool for generating graphical plots and visualizations. It provides a wide
range of built-in functions for creating different types of plots, such as line plots, scatter plots, bar
plots, histogram plots, and many others.
The basic syntax for creating a plot in MATLAB involves defining the data to be plotted and using
the appropriate plot function to generate the plot. For example, to create a line plot, the plot
function is used, while a scatter plot can be generated using the scatter function.
In addition to the basic plot functions, MATLAB provides a wide range of customization options
for fine-tuning the appearance and behavior of plots. This includes options for controlling line
styles, marker styles, axis limits, grid lines, legends, and many others. These options can be set
using various functions, such as set, xlabel, ylabel, title, legend, and many others.
MATLAB also provides a range of tools for analyzing and manipulating data prior to plotting.
This includes functions for filtering, smoothing, and resampling data, as well as tools for
computing statistics and performing various mathematical operations.
Overall, MATLAB provides a powerful and flexible platform for generating graphical plots and
visualizations, making it an essential tool for data analysis, scientific research, and engineering
applications.
1|Page
Haider Yaseen MEEN201101082
Curve Fitting:
Curve fitting in MATLAB is the process of finding a mathematical function that best fits a given
set of data points. It involves finding the best set of parameters that minimize the difference
between the model function and the observed data. Curve fitting is an important tool in data
analysis and is widely used in many fields, including engineering, physics, and economics.
MATLAB provides several built-in functions for curve fitting, including polyfit, lsqcurvefit, and
cftool. The polyfit function fits a polynomial curve to the data, while the lsqcurvefit function fits
an arbitrary function to the data using a least-squares optimization algorithm. The cftool function
provides a graphical interface for interactive curve fitting.
In addition to the built-in functions, MATLAB provides a wide range of tools for analyzing and
manipulating data prior to curve fitting. This includes functions for filtering, smoothing, and
resampling data, as well as tools for computing statistics and performing various mathematical
operations.
2|Page
Haider Yaseen MEEN201101082
1. Design the layout of the GUI using the GUI design tool.
3|Page
Haider Yaseen MEEN201101082
2. Add and customize the various graphical components such as buttons, sliders, and text
boxes.
3. Write callback functions to handle user interactions with the GUI.
4. Test the GUI and make any necessary modifications.
MATLAB provides a wide range of functions for customizing and controlling the behavior of GUI
components, such as set, get, uicontrol, and many others.
Task no 1:
Plot a set of data points using two or more variables from your lab experimentation o r
any semester project/ complex engineering problem/ assignment.
Code:
clc; % clear command window
4|Page
Haider Yaseen MEEN201101082
t= linspace(0,10,50);
f= log(t);
g=sin(t);
plot(t,f, t,g)
Task no 02:
5|Page
Haider Yaseen MEEN201101082
Plot a function as a curve using two or more variables from your lab experimentation or
any semester project/ complex engineering problem/ assignment. Add proper labels,
symbols and colours to the graph as well.
Code:
clc; % clear command window
clear all; % clear all previously defined varirables
t=[0:3];
y1=t;
y2=t.^.5;
y3=t.^2;
plot(t,y1,t,y2,t,y3)
xlabel=('time,s')
ylabel=('y(t)')
legend('y1','y2','y3')
title('y123 over time')
6|Page
Haider Yaseen MEEN201101082
Task no 03:
• Plot a set of curves in a single window using four or five variables from your lab
experimentation or any semester project/ complex engineering problem/ assignment.
Code:
clc; % clear command window
clear all; % clear all previously defined varirables
% Generate data
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
y3 = exp(-x);
y4 = x.^2;
% Plot curves
figure;
hold on;
plot(x, y1);
plot(x, y2);
plot(x, y3);
plot(x, y4);
7|Page
Haider Yaseen MEEN201101082
• Plot a set of curves (/ functions) in a multi window screen using subplot command
four or five variables from your lab experimentation or any semester project/
complex engineering problem/ assignment
Code:
clc; % clear command window
clear all; % clear all previously defined varirables
% Generate data
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
y3 = exp(-x);
8|Page
Haider Yaseen MEEN201101082
y4 = x.^2;
% Subplot 1
subplot(2, 2, 1);
plot(x, y1);
title('y1 = sin(x)');
xlabel('x');
ylabel('y');
% Subplot 2
subplot(2, 2, 2);
plot(x, y2);
title('y2 = cos(x)');
xlabel('x');
ylabel('y');
% Subplot 3
subplot(2, 2, 3);
plot(x, y3);
title('y3 = exp(-x)');
xlabel('x');
ylabel('y');
% Subplot 4
subplot(2, 2, 4);
9|Page
Haider Yaseen MEEN201101082
plot(x, y4);
title('y4 = x^2');
xlabel('x');
ylabel('y');
% Adjust spacing
sgtitle('Multiple Curves');
10 | P a g e
Haider Yaseen MEEN201101082
Task no 04:
Code:
x = [1, 2, 3, 4, 5];
% Polynomial degree
degree = 3;
figure;
hold on;
11 | P a g e
Haider Yaseen MEEN201101082
hold off;
grid on;
xlabel('x');
ylabel('y');
12 | P a g e
Haider Yaseen MEEN201101082
Task no 5:
Create a graph of a surface in 3-space using the following data:
• x = (0:2*pi/20:2*pi)'
• y = (0:4*pi/40:4*pi)'
• z = cos(X).*cos(2*Y)
Also, plot the mesh of given data along with the contour plot.
Code:
clc; % clear command window
clear all; % clear all previously defined varirables
x = (0:2*pi/20:2*pi);
y = (0:4*pi/40:4*pi);
[X,Y] = meshgrid(x,y);
Z = cos(X).*cos(2*Y);
contour(X,Y,Z)
13 | P a g e
Haider Yaseen MEEN201101082
Task no 06:
Design a GUI of a Basic Calculator with all the arithmetic operations, display of inputs
and output.
Code:
function varargout = matlab_tutorial_calculator(varargin)
% MATLAB_TUTORIAL_CALCULATOR MATLAB code for matlab_tutorial_calculator.fig
% MATLAB_TUTORIAL_CALCULATOR, by itself, creates a new
MATLAB_TUTORIAL_CALCULATOR or raises the existing
% singleton*.
%
14 | P a g e
Haider Yaseen MEEN201101082
15 | P a g e
Haider Yaseen MEEN201101082
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
16 | P a g e
Haider Yaseen MEEN201101082
% --- Outputs from this function are returned to the command line.
function varargout = matlab_tutorial_calculator_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
17 | P a g e
Haider Yaseen MEEN201101082
18 | P a g e
Haider Yaseen MEEN201101082
19 | P a g e
Haider Yaseen MEEN201101082
S= get(handles.screen,'string');
set(handles.screen,'string',strcat(S,'6'));
20 | P a g e
Haider Yaseen MEEN201101082
21 | P a g e
Haider Yaseen MEEN201101082
22 | P a g e
Haider Yaseen MEEN201101082
23 | P a g e
Haider Yaseen MEEN201101082
24 | P a g e
Haider Yaseen MEEN201101082
25 | P a g e