ap3114 lecture1 introduction jan14 2014

47
7/18/2019 AP3114 Lecture1 Introduction Jan14 2014 http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 1/47 AP3114/Computational Methods for Physicists and Materials Engineers Dr. Jun Fan Jan. 14, 2014 1. Typical Problems in Physics and Materials Engineering

Upload: ahmad-nawaz

Post on 10-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 1/47

AP3114/Computational Methods for

Physicists and Materials Engineers

Dr. Jun Fan

Jan. 14, 2014

1. Typical Problems in Physics

and Materials Engineering

Page 2: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 2/47

Problems in Math, Physics and

Materials Engineering

• 1. Linear equations• 2. Free Fall

• 3. Electric Current

• 4. Cantilever Beam

5. Diffusion• 6. Random Events

Page 3: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 3/47

1. Linear Equations

Scilab

Page 4: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 4/47

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

Page 5: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 5/47

2. Free Fall

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

Page 6: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 6/47

2. Free Fall

Page 7: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 7/47

2. Free Fall

Computation Results

Page 8: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 8/47

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:

Page 9: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 9/47

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)

Page 10: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 10/47

 4. Cantilever Beam

The Olive View Hospital

damaged in the 1971 San

Fernando earthquake (original

image from the EqIIS Collection) 

Page 11: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 11/47

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 verticaldisplacement from y = 0 under the loaded condition, with y thecoordinate in the direction opposite that of the load, can be writtenas follows:

where

E is a material property known as

the modulus of elasticity;

I is a geometric property of thecross-section of the beam known

as the moment of inertia; L is the

length; w is the load per unit width

of the beam.

Page 12: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 12/47

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

Page 13: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 13/47

5.

cdt 

dc   2

Diffusion equation:

1)0,0(     t  xc

0)0,(   t  xc

Page 14: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 14/47

5. Diffusion

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

2 ),(2),1(),1(),()1,( x

nicnicnic

nicnic

c(x,t)

x

t1

t3

t2

Results

cdt 

dc   2

Page 15: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 15/47

6. Random walk (Markov process)

• 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 student ending up at

home or in the café is he starts at a given corner.

Home Cafe

3 621 4 5P=1/3 P=2/3

Page 16: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 16/47

6. Random walk (Markov process)

• 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.

Home Cafe

3 621 4 5P=1/3 P=2/3

Transition probability matrix

Page 17: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 17/47

6. Random walk (Markov process)

Results: 48% at home, 52% at Cafe

Page 18: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 18/47

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)

Page 19: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 19/47

Scilab

• A large software package containing 13,000 files, more than400,000 lines of source code, 70,000 lines of Scilab code,80,000 lines of online help, and 18,000 lines ofconfiguration 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 – dynamicsystems modeler and simulator)

 – Signal processing

 – Random sampling and statisticas

 – Graphs, graphics, animation

Page 20: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 20/47

Programmable Calculator

• Scilab

 – www.scilab.org 

• Download (select a version for your own

computer, Windows/Mac/Linux) & install

 – http://www.scilab.org/download/5.4.1 

Windows Mac Linux

Page 21: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 21/47

Scilab interface

• Start -> Scilab

Page 22: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 22/47

HelpTo get help, type “help” 

> help derivative

Page 23: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 23/47

Editor

>editor

Save as *.sce file

To run: exec(filename.sce)

Page 24: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 24/47

Creating real variables

• Elementary mathematical operators

Page 25: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 25/47

Variable names

• Variable names may be as long as the userwants, but only the first 24 characters aretaken into account in Scilab.

• Case sensitive – “a” to “z”,

 – “A” to “Z”,

 –

“0” to “9”, – “%”, “_”, “#”, “!”, “$”, “?” 

 – Starting with “%” has special meaning, such as %pi 

Page 26: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 26/47

Case sensitive Variables

• For example

Page 27: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 27/47

Comments and continuation lines•

Comments: – Any line which begins with two slashes

 – “//” 

Continuation lines: – any line which ends with two dots

 – “..” 

Page 28: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 28/47

List of Elementary Mathematical Functioins

• Trigonometry

•Other functions

Page 29: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 29/47

Elementary mathematical functions

• For example,

 – Let’s test check it in Scilab:

1sincos   22   x x

Page 30: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 30/47

Pre-defined mathematical variables

• In Scilab, several mathematical variables are

pre-defined, whose name starts with a

percent “%” character. 

Page 31: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 31/47

Booleans

• Boolean variables can store

 – true (%t, or %T) or false (%f, or %F) values.

Page 32: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 32/47

Complex numbers

12i

In Scilab, %i represents the mathematical imaginarynumber i

?

“ ’ ” is used tocompute the

conjugate

“real ” returns

real part

“ imag ” returns

imaginary part

Page 33: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 33/47

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]

Page 34: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 34/47

Conversions between integers

• Types of integers returned by the inttype

function

• Scilab integer conversion functions:

Page 35: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 35/47

Circular integers and portability issues

Different from other mathematical packages, suchas Octave or Matlab: if an integer is at the upper

limit, the next integer stays at the upper limit

• The Scilab circular way allows for a greater flexibility in the

processing of integers, since it allows to write algorithms

with fewer if statements

Page 36: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 36/47

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

Page 37: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 37/47

Strings

• Strings can be stored in variables, provided that they aredelimited by double quotes “ ”. 

• “+” can operate strings. 

Page 38: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 38/47

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:

Page 39: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 39/47

Exercises (I)

Page 40: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 40/47

Exercises (II)

Page 41: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 41/47

Exercise (III)

What will you get by executing the following?

Page 42: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 42/47

Exercise (IV)

• What will you get by executing the following?

Page 43: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 43/47

Exercise (V)

• What will you get by executing the following?

Page 44: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 44/47

Exercise (VI)

• What will you get by executing the following?

Page 45: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 45/47

Exercise(VII)

• Determine the value of the following

expressions yourself before checking your

answers using Scilab

Page 46: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 46/47

Exercise(VIII)

• Given that a=[1 0 2] and b=[0 2 2], determine

the value of the following expressions. Check

your answers with Scilab

Page 47: AP3114 Lecture1 Introduction Jan14 2014

7/18/2019 AP3114 Lecture1 Introduction Jan14 2014

http://slidepdf.com/reader/full/ap3114-lecture1-introduction-jan14-2014 47/47

Reference

• http://www.scilab.org/resources/documentation/tuto

rials 

 – Introduction to Scilab (introscilab.pdf)

 – Scilab for very beginners (Scilab_beginners.pdf)