Vous êtes sur la page 1sur 8

DEPARTMENT OF CHEMICAL & MATERIALS ENGINEERING

CH E 374 Computational Methods in Engineering


FALL 2016

Seminar #1

Friday Sept. 9, 2016

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.

PART I: Interactive MATLAB


MATLAB Command Window
This is the main window in MATLAB which can be used for executing commands, running programs and
managing the software.
Some points to note about the command window

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

%Find the square root of a number


>> sqrt (64)
%Take the natural logarithm of a number
1

DEPARTMENT OF CHEMICAL & MATERIALS ENGINEERING

CH E 374 Computational Methods in Engineering


FALL 2016

Seminar #1

Friday Sept. 9, 2016

>>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. Define the variable as = 8.5 and then evaluate:


a) 4 3 14 2 6.32 + 7.3
b) log10 ( 2 3 )2
3. Define the variables , , as:
= 18.2, = 6.42, = and d = 0.5(cb + 2a), and evaluate:
a)

(+)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

DEPARTMENT OF CHEMICAL & MATERIALS ENGINEERING

CH E 374 Computational Methods in Engineering


FALL 2016

Seminar #1

Friday Sept. 9, 2016

Part II: Interactive with a script


1. Clear the workspace
>>clear
2. Using the Newtons problem from class example 1&2, you are going to evaluate the analytical solution
of a falling body.
The analytical expression is:
() =

(1 () )

(1)

First define the parameters required in equation (1)


>> g = 9.81;
>> m = 20e-3;
>> k = 0.015;
>> tf = 2;
now write the equation
>>% Exact solution
>> v = g*m/k * (1-exp(-k/m*tf))
Note the value of v echoed back on the screen and record it
[v = ...]
Lets check the variables in workspace
>>who
The computer should echo the variables typed earlier

Your variables are:


k g m tf v
To see the values of these variables
>> k
>>g
>>m
>>tf
3

DEPARTMENT OF CHEMICAL & MATERIALS ENGINEERING

CH E 374 Computational Methods in Engineering


FALL 2016

Seminar #1

Friday Sept. 9, 2016

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.

5. Create a file named analytic_sol.m using the MATLAB editor.


>> edit analytic_sol
Type the parameters and expression as shown in Figure 1.

DEPARTMENT OF CHEMICAL & MATERIALS ENGINEERING

CH E 374 Computational Methods in Engineering


FALL 2016

Seminar #1

Friday Sept. 9, 2016

Figure 1: Typing your first script in MATLAB editor.


Save the script and go back to the command prompt.
You can now invoke the script by calling its name
>> analytic_sol
v=
10.1615
We can make the script more interactive /user friendly by making it ask for any parameter to be changed. For
example, we could make the script request for the mass of the body before calculating the velocity.
Go back to the Editor window and modify the script to include user input.

DEPARTMENT OF CHEMICAL & MATERIALS ENGINEERING

CH E 374 Computational Methods in Engineering


FALL 2016

Seminar #1

Friday Sept. 9, 2016

Figure 2: Script with user input


Save the program with a different name, e.g. analytic_sol2
Run the script with m = 50 g.
>> analytic_sol2
mass = (kg): 50e-3
v=
14.7539

DEPARTMENT OF CHEMICAL & MATERIALS ENGINEERING

CH E 374 Computational Methods in Engineering


FALL 2016

Seminar #1

Friday Sept. 9, 2016

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);

Save the program.


You can now invoke the numerical solution from the command window:
>> numer_sol
mass = (kg): 50e-3
The velocity is 14.9179 (m/s)>>
PART III Creating Plots in MATLAB
Lets make some plots.
Create a data vector
>>x=[0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0];
>>y=[0 4.91 7.97 9.89 11.08 11.83 12.30 12.59 12.78 12.89 13.01];
>> plot (x,y,'r*-')
Lets put some annotations to the graph to make it pretty.
>> grid on
>> title ('This is my first graph')
>> xlabel ('time, seconds')
>> ylabel ('velocity, m/s')

DEPARTMENT OF CHEMICAL & MATERIALS ENGINEERING

CH E 374 Computational Methods in Engineering


FALL 2016

Seminar #1

Friday Sept. 9, 2016

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