MATLAB Basics: CHE 374 Computational Methods in Engineering
MATLAB Basics: CHE 374 Computational Methods in Engineering
MATLAB Basics
3 4
>> A =[1 3; 7 9]
A=
1 3
7 9
The function whos lists the size and memory allocation of your
variables
>> whos
Name Size Bytes Class Attribute
A 2x2 32 double
M 1x2 16 double
ans 1x1 8 double
m 1x3 24 double
z 1x1 16 double complex
11 12
>> clear A
>> who
>> clear
>> who
Your variables are:
>>
13 14
Output formats
The function format changes the precision of the output Output formats
Command Description Example Command Description Example
format short Fixed-point with 4>> 355/113 format short g Best of 5-digit fixed or >> 355/113
decimal digits ans = floating point ans =
3.1416 3.1416
format long Fixed-point with 15 >> 355/113 format long g Best of 15-digit fixed or >> 355/113
decimal digits ans = floating point ans =
3.141592920353983 3.14159292035398
format short e Scientific notation >> 355/113 format bank Two decimal digits >> 355/113
with 4 decimal ans = ans = 3.14
digits 3.1416e+000 format rat Rational >> pi
format long e Scientific notation >> 355/113 ans = 355/113
with 15 decimal ans =
digits 3.141592920353983e+00015 16
Computations on integer variables are done natively in integer. Integer >> b=[2;7];
variables are always displayed to the appropriate number of digits for the >> A*b
class, for example, 3 digits to display the INT8 range -128:127. ans =
30
54
FORMAT SHORT and LONG do not affect the display of integer variables.
……
17 18
12
>> xlabel (‘text’) add xlabel
The side graph is nice, 10
presentable by adding lower left corner and (1.0, 1.0) is the upper right
titles and labels
2
corner of the screen.
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
23 24
Adding tittle and axes labels Axis changes, subplots, gridlines, hold and zoom
>> subplot divides the plot window
>> title ('This is my first graph')
>> xlabel ('time, seconds')
This is my first graph
>> ylabel ('velocity, m/s') 14 >> axis change axes
12
velocity, m/s
8
>> grid adds grid lines
6
4
>> hold allows you to make multiple plots
on the same subplot
2
Approximate Solution
exact1 =
10 In the previous plot, we included a plot of a function
v e lo c ity , m /s
Exact Solution
@(t)13.1*(1-exp(-0.75*t))
5
>> hold on
% define an anonymous function
0
0 1 2 3 4 5 >> fplot (exact1, [0 5]) >> exact1 = @(t) 13.1*(1-exp(-0.75*t))
time, seconds
>> grid on >> fplot (@exact1, [0 5] ) % plot from t=0 to 5
>> title ('This is my first graph') >> grid on
>> xlabel ('time, seconds')
>> ylabel ('velocity, m/s')
>> gtext('Exact Solution')
>> gtext('Approximate Solution')
27 28
y
>> subplot (2,1,1)
>> plot(x,y); -1
y
>> title('plot of y/(x-2) vs x');
-0.5
>>
-1
0 1 2 3 4 5 6 7 8 9 10
x
29 30
31 32
factorial = factorial =
2.4329e+018 2.4329e+018
33 34
function
Following is a function which does the same thing as the script.
Miscellaneous commands
function vel = fun(dt,ti,tf,vi,m,k) To run the script,
g = 9.81; %gravitational const. type at the
t = ti; %set t to start time command prompt
v = vi; %set v to initial
h = dt; %set the time steps >> m=20e-3;
while (t <= tf) % loop until t> tf >> k=0.015;
slope = g -(k/m) * v; >> ti=0;
v = v + slope * h; >> tf=3.0;
t = t + h; >> vi=0;
end >> dt=0.1;
vel = v; >> fun(dt,ti,tf,vi,m,k)
end
37 38
39