Download presentation
Presentation is loading. Please wait.
1
Computer Application in Engineering Design
Lecture No 03 Engr. Aamir Arsalan
2
Plotting Plotting is a very important and powerful feature in MATLAB.
3
Example
4
Activity In the Command window (or use the Script Editor) in MATLAB window input the time from t=0 seconds to t=10 seconds in increments of 0.1 seconds as follows: Then, compute the output y as follows: Use the Plot command:
5
Plotting Multiple Data Points in One Graph
In MATLAB it is easy to plot multiple data set in one graph.
6
Hold Command Another approach is to use the hold command.
7
Activity Given the following differential equation:
where , where T is the time constant. The solution for the differential equation is: Set T=5 and initial condition x(0)=1. Create a Script in MATLAB (.m file) where you plot the solution x(t) in the time interval ๐โค๐โค๐๐ Add Grid, and proper Title and Axis Labels to the plot.
8
Subplot Command The subplot command enables you to display multiple plots in the same window or print them on the same piece of paper. Syntax is:
9
Example
10
Plot Sin(x) and Cos(x) in 2 different subplots. Add Titles and Labels.
Activity Plot Sin(x) and Cos(x) in 2 different subplots. Add Titles and Labels.
11
Customizing Plots There is lots of customizing you can do with plots, e.g., you can add a title, x- and y-axis labels, add a legend and customize line colors and line-styles. The functions for doing this is; title, xlabel, ylabel, legend, etc.
12
Line Styles For line colors and line-styles we have the following properties we can use for the plot function:
13
Marker Specifiers
14
Colors
15
Example
16
Flow Control and Loops You may use different loops in MATLAB โข For loop โข While loop If you want to control the flow in your program, you may want to use one of the following: โข If-else statement โข Switch and case statement
17
If-else Statement
18
If else Statements If A and B are scalars this works โ but If A and B are matrices this might not work as expected! Use instead:
19
Operators
20
Logical Operators
21
Activity Create a function that finds the solution for x based on different input values for a, b and c.
22
Switch and Case Statements
The switch statement executes groups of statements based on the value of a variable or expression. There must always be an end to match the switch. The general syntax is as follows:
23
Activity Create a function that finds either the Area or the circumference of a circle using a Switch Case statement
24
For Loop The For loop repeats a group of statements a fixed, predetermined number of times. A matching end delineates the statements. The general syntax is as follows:
25
Activity In mathematics, Fibonacci numbers are the numbers in the following sequence: 0, 1, 1, 2 ,3, 5, 8, 13, 21, 34, 55, 89, 144, โฆ Write a function in MATLAB that calculates the N first Fibonacci numbers
26
While Loop The while loop repeats a group of statements an indefinite number of times under control of a logical condition. A matching end delineates the statements. The general syntax is as follows:
27
Activity Create a Script or Function that creates Fibonacci Numbers up to a given number.
28
Activity Extend your calc_average function from a previous task so it can calculate the average of a vector with random elements. Use a For loop to iterate through the values in the vector and find sum in each iteration: mysum = mysum + x(i); Test the function in the Command window.
29
Activity Create a function where you use the โif-elseโ statement to find elements larger than a specific value in the task above. If this is the case, discard these values from the calculated average. Example discarding numbers larger than 10 gives:
30
Questions??
31
Thank You!!! ๏
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.