Chapter 1n
Chapter 1n
Chapter 1n
Computing Skills
Author
image here
Chapter 1: Introduction to Matlab
Chapter 2: Algorithmic Structures in
MATLAB
Chapter 3: Mathematical Applications in
MATLAB
Outline
Chapter 4: Polar Coordinates in MATLAB &
Integration Using Polar Coordinates
Chapter 5: Definite and Indefinite Integrals
Chapter 6: Symbolic Math in MATLAB
Chapter 1: Introduction to Matlab
Author
image here
Introduction
Matlab for Windows
Variables
Matlab as a Calculator
Constants and Math Functions
Outline Display Function & Formats
Plots
Programming in Matlab
Scripts
Functions
Matlab
A program for doing numerical computation. Originally
designed for solving linear algebra type problems
using matrices.
5
Display Windows
6
Matlab GUI
Desktop menus
File menu
New
Create new Matlab program file (m-
file)
Open existing m-file
Import data
Set path
Open recent m-files
7
MATLAB GUI
Edit Menu
Copy, cut, paste
Find and replace
phrases
Clear command
history, workspace
Desktop Menu
Change appearance of
desktop
Select windows to
display
8
Getting Help
9
Arithmetic Operators in MATLAB
The contents of all bracketed expressions are evaluated, starting from the
innermost brackets and working out.
All exponentials are evaluated, working from left to right.
All multiplications and divisions are evaluated, working from left to right.
All additions and subtractions are evaluated, working from left to right.
Operators:
+ for addition
- For subtraction
* for multiplication
/ for division
^ for power
10
Operator Precedence
13
Variable Examples
14
Exercise
15
String Variables
>> a + b
??? Error using ==> plus Matrix dimensions must agree.
16
Special Variables
17
Defining Symbolic Variables & Viewing Variables
19
MATLAB as a Calculator
>> eps
ans = 2.2204e-016
>> sin(pi/2)
ans = 1
>> log(1000)
ans = 6.9078
>> log10(1000)
ans = 3
21
Elementary Math Functions
24
Rounding Examples
+ -
round(2.7) = ? round(-2.7) = ?
round(2.3) = ? round(-2.3) = ?
fix(2.3) = ? fix(-2.3) = ?
fix(2.7) =? fix(-2.7) = ?
ceil(2.3) = ? ceil(-2.3) = ?
ceil(2.7) = ? ceil(-2.7) = ?
floor(2.3) = ? floor(-2.3) = ?
floor(2.7) = ? floor(-2.7) = ?
rem(13,5) = ? rem(13,5) = ?
25
Mathematical Functions in MATLAB
28
fprintf arguments
Type of variables:
%i: integer
%d: double
%f: float
%c: char
%s: string
Possibility to control the number of decimal places
Ex: fprintf(‘a with three decimal places: %.3f',a)
Ex: fprintf('a with 4 numbers: %.4i',a)
All the text in the same line? Use \n for a new line
29
Drawing in MATLAB
In Matlab, we can plot a symbolic function over one variable by using the
ezplot function.
>> ezplot(y)
>> f = sin(x);
>> ezsurf(f) % another type of plotting example
>> g = cos(y);
>> ezsurf(f+g);
32
subplot Command
syms x
subplot(2,2,1)
ezplot(cos(x))
subplot(2,2,2)
ezplot(sin(x))
subplot(2,2,3)
ezplot(tan(x))
subplot(2,2,4)
ezplot(atan(x)) 33
Programming in MATLAB
Script and Functions: Script Files
35
Create a .m File
36
Example 1 Example 1:
in m-file: in Command window
A= 2; >> example1
B=3;
C=A+B C = ?
37
Example 2
m = 10;
a = 100;
Force =m * a
A m-file also , like a script file, except that the variables are all local.
A function file begins with a function definition line containing a well-
defined list of inputs and outputs.
Without that line, the file becomes a simple script file.
The syntax of the function definition line is:
function [ output variables ] = function_name ( input
variables );
It is recommended for the function_name to be the same as the
filename (without the ‘.m’ extension) in which the function is written.
Example: if the name of the function is projectile it must be written
and saved in a file with the name projectile.m.
39
WARNING!
for the scipt or function name
file, avoid using the name of
a MATLAB command such as
plot, sqrt, abs…
Example 3
Write a MATLAB function called sumy that adds two input numbers and
returns the result minus 2
41
Example 3: Measuring a Solid Object Solution
Function file:
We need to compute the volume and function [area, volume] = cyl(h, r)
surface area of this cylinder object: % function to compute the area and volume of a cylinder
base = pi * r^2;
volume = base * h
area = 2*pi*r * h + 2*base
42
Exercises
Example 1: Create a m-file and enter the
Consider the system of equations: following statements in the m-file:
x + 2y + 3z = 1
A = [1 2 3; 3 3 4; 2 3 3];
3x + 3y + 4z = 1
b = [1; 1; 2];
2x + 3y + 3z = 2
x = A\b
Find the solution x to the system of
equations.
Save the file, (e.g. example1.m)
In the command line, type:
>> example1
x = ?
44
Example 2: Create a file, say example2.m,
Plot the following cosine functions, which contains the following
y1 = 2 cos(x) commands:
y2 = cos(x)
x = 0:pi/100:2*pi;
y3 = 0.5 * cos(x)
y1 = 2*cos(x);
in the interval 0 x 2. y2 = cos(x);
y3 = 0.5*cos(x);
plot(x,y1,'--',x,y2,'-',x,y3,':')
xlabel('Range')
ylabel('Cosine functions')
legend('2*cos(x)','cos(x)','0.5*cos(x)')
title('Example of multiple plots')
axis([0 2*pi -3 3])
b=5;
h=3;
area=0.5*(b*h)
area = ?
47
Scripts VS Functions
function a = triarea(b,h)
a = 0.5*(b.* h);
After saving the file, call the function with different values
a1 = triarea(1,5)
a1 = ?
a2 = triarea(2,10)
a2= ?
a3 = triarea(3,6)
a3= ?
49
Functions have their own
workspace (local), separate from
the base workspace (global). So,
none of the calls to the function
triarea overwrite the value of a in
the base workspace. Instead, the
function assigns the results to
variables a1, a2, and a3.