Beruflich Dokumente
Kultur Dokumente
MATLAB basics
12/17/15
4 5 = *; 6 5 = !
* 5 = 8*; ! 5 = 9;
12/17/15
Result
MATLAB Code
%% Define Parameters and Initial Conditions
param.g
= 9.81;
% gravitational acceleration
param.kappa = 0.006;
% air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);
%% Setting up and Solving the problem
X0 = [0; 0;
% starting position is the origin
u0; v0];
% starting velocity is given
tSpan = [0 20];
% simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [], param);
%% Displaying the results
figure(1);
plot(XOut(:,1),XOut(:,2),'bo');
xlabel('x (m)'); ylabel('y (m)');
%% Animating results
exitCode = ballAnimation(tOut,XOut);
12/17/15
Output block
Computation
Input block
Comment
Assignment
(Math) Expression
Calling a function
plot(XOu
Calling a function
12/17/15
MATLAB Code
%% Define Parameters and Initial Conditions
param.g
= 9.81;
% gravitational acceleration
param.kappa = 0.006;
% air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);
%% Setting up and Solving the problem
X0 = [0; 0;
% starting position is the origin
u0; v0];
% starting velocity is given
tSpan = [0 20];
% simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [], param);
%% Displaying the results
figure(1);
plot(XOut(:,1),XOut(:,2),'bo');
xlabel('x (m)'); ylabel('y (m)');
%% Animating results
exitCode = ballAnimation(tOut,XOut);
Assigning variables
Arrays / Matrices
Suppress echo
Variables are case-sensitive
12/17/15
Scalar Operations
+ - * / ^
log, exp
pow, sqrt
sin, cos, tan
asin, acos, atan
rem, round, ceil, floor
Variable
Meaning
pi
Number /
eps
Machine precision
Imaginary unit
inf
Infinity
NaN
ans
end
realmax
intmax
Largest integer
12/17/15
Building arrays
Colon notations
12/17/15
Building Arrays
Array Building Functions
Recall that we can build arrays as:
Command
Meaning
ones(m,n)
Build mn matrix of 1s
zeros(m,n)
Build mn matrix of 0s
eye(n)
Identity matrix
diag(vec)
diag(A)
Diagonal elements of A
rand(m,n)
randn(m,n)
magic(m)
hilb
Hilbert matrix
Matrix Operations
+
log, exp
logm, expm
power, sqrt
mpower, sqrtm
sum,prod,cumsum,cumprod
12/17/15
Matrix Operations
.*
./
.^
log, exp
logm, expm
power, sqrt
mpower, sqrtm
sum,prod,cumsum,cumprod
12/17/15
Math
Programming
Thermodynamics
Mechanics
Amit
24
44
36
36
Bhavna
52
57
68
76
Chetan
66
53
69
73
Deepak
85
40
86
72
Elizabeth
15
47
25
28
Farah
79
72
82
91
10
12/17/15
Extract row 3 and give the marks to Chetan. Also calculate his total marks
We will use matrix fundaes for this:
Extract marks of our best students, Deepak and Farah for first three courses
< =
2< 0.1=
Calculate average marks obtained in each of the four courses
> ? 2 0 = 2> 0.1?
@ A 0 0.1
2@ 0.1A
Scale all the marks out of 10*
11
12/17/15
execute 10 times)
for i=1:10
<statement 1>;
<statement 1>;
<statement n>;
<statement n>;
end
i=i+1;
end
12
12/17/15
condition is met
13
12/17/15
MacLaurin Series
14
12/17/15
http://in.mathworks.com/videos/writing-a-matlab-program-69023.html
15
12/17/15
Functions
Files that take certain input(s),
executes sequence of steps, and
returns output(s) at the end
Scope of Variables
script shares the variables with workspace from where it was called
Typically, that means MATLAB workspace
16
12/17/15
G! = 12G
Pass on the function values to MATLAB function for solving something; e.g.,:
\]
\$
= A H, 6 function dy = myODEfun(t,y)
<...>ode45(@myOdefun, <...>)
All other purposes, you are likely to use scripts (instead of functions)
17
12/17/15
Other options
Plotting data
Other options
19
12/17/15
disp(b)
disp(Hello world)
Plotting
Consider the example of a ball thrown vertically upwards
20
12/17/15
MODULE 1
INTRODUCTION TO MATLAB PROGRAMMING
Dr. Niket Kaisare
Department of Chemical Engineering
IITMadras
NPTEL Course: MATLAB Programming for Numerical Computations Week-1
21
12/17/15
Summary of Module-1
MATLAB basics
Summary of Module-1
Execution control
if-then statements
MATLAB files
Plotting in MATLAB
22