Академический Документы
Профессиональный Документы
Культура Документы
Seminar #1
The objective of these exercises is to familiarize you with MATLAB. You are going to define some scalars,
vectors and matrix variables and carry out some basic operations with them. You will also learn how to generate
simple graphs, write MATLAB scripts and functions and execute them from within MATLAB.
Do not simply follow the steps outlined here blindly. Try to understand what you have done after each step, and
observe how and why the computer has responded the way it has.
To type a command, the cursor must be placed next to the command prompt (>>)
A command is executed after the Enter key is pressed
A previously typed command can be recalled to the command prompt using the up arrow key ()
At any point, you can type >> help <command> to obtain on-line help.
Example: To obtain help on MATLAB arithmetic operations, type
>> help arith
%The largest positive floating point number
>> realmax
%The largest negative floating point number
>> realmin
%Floating point relative accuracy
>> eps
Seminar #1
>>log (6.0)
%To evaluate the sine of a number
>>sin(pi/4)
%Factorial of a number
>>factorial(7)
%Round a number towards the nearest integer
>> round(22/7)
%Find the remainder when a number is divided
>>rem(22,3)
Solve the following problems by writing commands in the command window.
1. Calculate the following
a)
29.543 1200
112 +30.2
(+)2
||
4. The temperature dependence of vapour pressure p can be estimated by the Antoine equation:
ln() =
Where ln is the natural logarithm, p in im mm Hg, T is in Kelvin, and A, B, and C are material constants.
Calculate the vapour pressure of Methane at 300 and 400 K. Material constants at this temperature range are
= 16.0137, = 3096.52 = 53.67.
P300 =.
P400 =.
2
Seminar #1
(1 () )
(1)
Seminar #1
Now that you have assured that all variables typed are still available in memory. These values will remain in the
memory until cleared and can therefore be used in any expression within the current session. Thus, if we wished
to compute the velocity at a different time, we need to change the value of tf, and then type the equation again.
3. Assign a new value to tf
>> tf = 4
4. Scroll up the command history using the up arrow key until you see the line with equation >> v = g*m/k
* (1-exp(-k/m*tf)). Press Enter to execute the function again.
[v = .]
It is obvious that it is going to be cumbersome to retype the equation over and over again each time we need to
use it. To overcome this problem, we can write all the commands and statements required to solve the problem
in a file, and ask MATLAB to execute the commands as needed. This can be accomplished by using a
MATLAB script. {See page 18 in the Lab Manual for the description of MATLAB scripts and functions}.
Create a working directory
You will need a space to organize your files. Create a folder in your computer desktop. Name the folder
seminar1LASTNAME
Change the working directory to the newly created folder (seminar1LASTNAME). All files created will be
saved in the current working folder.
Seminar #1
Seminar #1
Seminar #1
Our next exercise is to write the numerical solution of the falling sphere in a script. We shall modify
analytic_sol2.m and call it numer_sol.
% My Second script
% Numerical solution of the falling paratrooper
% Uses Euler's method
%
g = 9.81; % gravitational constant
m = input ('mass = (kg): ');
k = 0.015; % drag coefficient (-)
ti = 0;
% initial time
vi = 0;
% initial velocity (at time ti)
tf = 2;
% final time, (s)
dt = 0.1; % time step
t = ti;
% start at the initial time
v = vi;
% set the initial velocity
%loop until the desired time is reached
while (t <= tf)
slope = g - (k/m) *v;
v = v + slope * dt;
t = t + dt;
end
fprintf('The velocity is %g (m/s)',v);
Seminar #1
Now lets add another curve in the same Figure. We can add a different curve to an existing figure by keeping
the current figure on hold while we add the new curve(s).
Keep the current graph on hold
>> hold on
Now create another graph. This time we are going to create a graph from a function by using the command
fplot.
First we define the function. {We can create a user defined function, an inline function or anonymous function
functions to be detailed later.}. Lets create an anonymous function
>> exact1 = @(t) 13.1*(1-exp(-0.75*t))
exact1 =
@(t)13.1*(1-exp(-0.75*t))
The function has been created. Now plot the function in the span t = 0 to 5 seconds.
>> fplot(exact1,[0 5])
We can add annotation to identify the curves
>> gtext('Exact Solution')
>> gtext('Approximate Solution')
This is my first graph
14
Approximate Solution
12
Exact Solution
velocity, m/s
10
0
0
0.5
1.5
2
2.5
3
time, seconds
3.5
4.5