You are on page 1of 50

AP3114/Computational Methods for

Physicists and Materials Engineers


1. Typical Problems in Physics
and Materials Engineering
& Introduction to Scilab
Dr. Jun Fan
Aug. 31, 2015

Questions about you


Which year are you in, 1, 2, 3, or 4?
Do you have any programming experience?
// year 1 2 3 and 4
clf; x=[1, 2, 3, 4];
// number of students in year 1/2/3/4
n1=[2, 12, 16, 20];
// number of students who have
programming experience
n2=[0 5 8 16];
bar(x, [n1', n2'])
xlabel('Year','fontsize',6)
ylabel('No. of students','fontsize',6)

Problems in Math, Physics and


Materials Engineering

1.
2.
3.
4.
5.
6.

Linear equations
Free Fall
Electric Current
Cantilever Beam
Diffusion
Random Events

1. Linear Equations

Scilab

2. Free Fall
t=0, v=0,
Case 1. No air resistance
Case 2. Resistance proportional to
the linear power of velocity
Case 3. Resistance proportional to
the second power of the velocity

2. Free Fall
What is the status of the particle at
t=5?
(Given k=0.2, g=9.81m/s2)

2. Free Fall

2. Free Fall
Computation Results

Case 1
Case 2
Case 3

Case 1
Case 2
Case 3

3. Electric Current
A single, closed-loop electrical circuit
The loop contains

a voltage source V (battery),


a resistor R (energy dissipation device),
an inductor L (an energy storage device),
A switch that is instantaneously closed at time
t=0

Solution:

3. Electric Current
V=1; R=1; L=1;
t=0:0.01:6;
i=(V/R)*(1-exp(-R.*t/L));
figure;plot(t,i,'ro');
xlabel('time,
t','fontsize',6),
ylabel('current,
i','fontsize',6)

4. Cantilever Beam
The Olive View Hospital
damaged in the 1971 San
Fernando earthquake
(original image from the
EqIIS Collection)

4. Cantilever Beam
For a uniformly loaded span of a cantilever beam attached
to a wall at x = 0 with the free end at x = L, the formula
for the vertical displacement from y = 0 under the loaded
condition, with y the coordinate in the direction opposite
that of the load, can be written as follows:

where
E is a material property known as
the modulus of elasticity;
I is a geometric property of the
cross-section of the beam known
as the moment of inertia; L is the
length; w is the load per unit width
of the beam.

4. Cantilever beam
x=0:0.01:1;
y=-(x.^4-4*x.^3+6*x.^2);
plot(x,y)
xlabel('x','fontsize',6)
ylabel('y','fontsize',6)
title('Cantilever Beam')
plot([0 1],[0 0],'linewidth',2)
legend('Uniformly loaded
beam,unloaded)

5.

Diffusion equation:
dc
2c
dt

c( x 0, t 0) 1
c( x, t 0) 0

5. Diffusion
dc
2c
dt

c(i, n 1) c(i, n) c(i 1, n) c(i 1, n) 2c(i, n)

t
x 2

x=1:1:100; dt=0.05;
c=zeros(100,1); c(1)=1;
figure;
for t=1:1:3000
for i=2:1:99
d(i)=(c(i+1)+c(i-1)2*c(i))*dt+c(i);
end
d(100)=d(99);
d(1)=1-sum(d(2:100));
c=d;
if pmodulo(t,1000)==0
then plot(x,c);
end
end

Results
t1
c(x,t)

t2
t3
x

6. Random walk (Markov


process)
Cafe
Home
1

P=1/3 3 P=2/3

A drunk wanders down a street that has six


intersections. His home is at interaction 1, and his
favorite Internet caf is at intersection 6.
At each intersection other than his home or the
caf, he moves in the direction of the caf with
probability 2/3 and in the direction of his home with
probability 1/3.
We would like to know the chances of the drunk
ending up at home or in the caf is. He starts at a

6. Random walk (Markov


process)
Cafe
Home
1

P=1/3 3 P=2/3

We represent the probabilities of being in these


states a six-component state vector X(t), where Xi(t)
is the probability of him being at intersection i at
moment t. The components of X(t) must sum to 1,
since he has to be in one of these states.

Transition probability

6. Random walk (Markov


process)

Results: 48% at home, 52% at


Cafe

Scientific software
Two categories of general scientific
software
Computer algebra systems that perform
symbolic computations: Maple,
Mathematica, Maxima, Axiom, and
MuPad
General purpose numerical systems
performing numerical computations and
designed specifically for scientific
applications: Matlab, Scilab (free)

Scilab
A large software package containing 13,000 files, more
than 400,000 lines of source code, 70,000 lines of
Scilab code, 80,000 lines of online help, and 18,000
lines of configuration files, including
Elementary functions of scientific calculation
Linear algebra, spare matrices
Polynomials and rational functions
Classic and robust control, optimizations
Nonlinear methods (optimization, ODE solver, Scicos
dynamic systems modeler and simulator)
Signal processing
Random sampling and statistics
Graphs, graphics, animation

Programmable Calculator
Scilab
www.scilab.org

Download (select a version for your


own computer, Windows/Mac/Linux)
& install
http://www.scilab.org/download/5.5.1
A copy for Windows has been upload to
Canvas
Windows

Mac

Linux

Reference
http://www.scilab.org/resources/documentati
on/tutorials
Introduction to Scilab (introscilab.pdf)
Scilab for very beginners
(Scilab_beginners.pdf)

Scilab interface
Start -> Scilab

Variable
s
Director
y
&
files

Comma
nd Input
Interfac
e
Comma
nd
history

Help
To get help, type help
> help derivative

Editor
>editor
Save as *.sce
file
To run:
exec(filename.sce)

Who is this?

Ihear, I forget
Isee, I remember
Ido, I learn
26

Creating real variables

Elementary mathematical operators

Variable names
Variable names may be as long as the user
wants, but only the first 24 characters are
taken into account in Scilab.
Case sensitive
a to z,
A to Z,
0 to 9,
%, _, #, !, $, ?
Starting with % has special meaning, such as
%pi

Case sensitive Variables


For example

Comments and continuation


lines
Comments:
Any line which begins with two slashes
//

Continuation lines:
any line which ends with two dots
..

List of Elementary Mathematical


Functions
Trigonometry

Other functions

Elementary mathematical functions


For example,cos x sin x 1
2

Lets test check it in Scilab:

Pre-defined mathematical variables


In Scilab, several mathematical
variables are pre-defined, whose
name starts with a percent %
character.

Booleans
Boolean variables can store
true (%t, or %T) or false (%f, or %F)
values.

Complex numbers
In Scilab, %i represents the mathematical
imaginary number i

i 1?
2

is used
to compute
the
conjugate

real
returns real
part
imag
returns
imaginary

Integers
Integer type:
Number of bits used to store an
integer and the range of value:
n-bit signed integer: [-2n-1, 2n-1-1]
n-bit unsigned integer: [0, 2n-1]

Conversions between
integers
Scilab integer conversion
functions:

Types of integers returned by the


inttype function

Circular integers and portability


issues

The Scilab circular way allows for a greater


flexibility in the processing of integers, since it
allows to write algorithms with fewer if
statements

Different from other mathematical


packages, such as Octave or Matlab: if
an integer is at the upper limit, the next
integer stays at the upper limit

The ans variable


When we make a computation and do not store
the result into an output variable, the result is
stored in the default ans variable

Strings
Strings can be stored in variables, provided that
they are delimited by double quotes .
+ can operate strings.

Dynamic type of variables


Scilab allows to change the type of a variable
dynamically.
The following example shows that we first
create a real value, and then put a string
variable in it:

Home Work-1
Due: 9am, Sep 7, 2015
Late policy: 10% off per day
To be submitted online to Canvas.

Exercises (I)

Exercises (II)

Exercises (III)
What will you get by executing the following?

Exercises (IV)
What will you get by executing the
following?

Exercises (V)
What will you get by executing the
following?

Exercises (VI)
What will you get by executing the
following?

Exercises (VII)
Determine the value of the following
expressions yourself before checking
your answers using Scilab

Exercises (VIII)
Given that a=[1 0 2] and b=[0 2 2],
determine the value of the following
expressions. Check your answers
with Scilab

You might also like