Vous êtes sur la page 1sur 34

PROJECT 1

1. User interface
2. Options for the user to pick and based on
the response you compute roots of the
equation.
1
YOU WANT GIVE USER TO PROVIDE INPUT
BEFORE YOU PROCEED
help input
INPUT Prompt for user input.
R = INPUT('How many apples') gives the user the prompt in the
text string and then waits for input from the keyboard.
The input can be any MATLAB expression, which is evaluated,
using the variables in the current workspace, and the result
returned in R. If the user presses the return key without
entering anything, INPUT returns an empty matrix.

R = INPUT('What is your name','s') gives the prompt in the text
string and waits for character string input. The typed input
is not evaluated; the characters are simply returned as a
MATLAB string.

The text string for the prompt may contain one or more '\n'.
The '\n' means skip to the beginning of the next line. This
allows the prompt string to span several lines. To output
just a '\' use '\\'.
2
HOW TO PROVIDE USER VARIOUS CHOICES?
CASE SWITCH statement case.
CASE is part of the SWITCH statement syntax, whose general
form is:

SWITCH switch_expr
CASE case_expr,
statement, ..., statement
CASE {case_expr1, case_expr2, case_expr3,...}
statement, ..., statement
...
OTHERWISE,
statement, ..., statement
END
3
MULTIPLE USER INPUTS
(1) Asked what method you want ?
(2) Now provide me guess values ?
(3) Now provide me stopping criteria?
4
DIRECT METHOD OF
INTERPOLATION

Chapter 18: 18.1
5
DIRECT METHOD OF
INTERPOLATION





6
WHAT IS INTERPOLATION ?
7
Given (x
0
,y
0
), (x
1
,y
1
), (x
n
,y
n
), find the value of y at a
value of x that is not given.

Figure 1 Interpolation of discrete.

INTERPOLANTS
Polynomials are the most common
choice of interpolants because they
are easy to:
8
Evaluate
Differentiate, and
Integrate
DIRECT METHOD
Cubic
Quadratic
linear
3
3
2
2 1 0
2
2 1 0
1 0
.
x a x a x a a y
x a x a a y
x a a y
+ + + =
+ + =
+ =
9




EXAMPLE 1:LINEAR INTERPOLATION
The upward velocity of a rocket is given as a
function of time in Table 1.
Find the velocity at t=16 seconds using the
direct method for linear interpolation.



10
0 0
10 227.04
15 362.78
20 517.35
22.5 602.97
30 901.67
Table 1 Velocity as a function
of time.
Figure 2 Velocity vs. time data for the
rocket example
( ) s , t ( ) ( ) m/s , t v
LINEAR INTERPOLATION
11
( ) t a a t v
1 0
+ =




( )
0 0
, y x
( ) x f
1
( )
1 1
, y x
x
y
Figure 3 Linear interpolation.
LINEAR INTERPOLATION
12
( ) t a a t v
1 0
+ =
( ) ( ) 78 . 362 15 15
1 0
= + = a a v
( ) ( ) 35 . 517 20 20
1 0
= + = a a v



( )
0 0
, y x
( ) x f
1
( )
1 1
, y x
x
y
Figure 3 Linear interpolation.
LINEAR INTERPOLATION
13
( ) t a a t v
1 0
+ =
( ) ( ) 78 . 362 15 15
1 0
= + = a a v
( ) ( ) 35 . 517 20 20
1 0
= + = a a v
Solving the above two equations gives,

93 . 100
0
= a

914 . 30
1
= a


( )
0 0
, y x
( ) x f
1
( )
1 1
, y x
x
y
Figure 3 Linear interpolation.
LINEAR INTERPOLATION
14

93 . 100
0
= a

914 . 30
1
= a
Hence
( ) . 20 15 , 914 . 30 93 . 100 s s + = t t t v

( ) m/s ? 16 = v

( )
0 0
, y x
( ) x f
1
( )
1 1
, y x
x
y
Figure 3 Linear interpolation.
LINEAR INTERPOLATION
15

93 . 100
0
= a

914 . 30
1
= a
Hence
( ) . 20 15 , 914 . 30 93 . 100 s s + = t t t v

( ) ( ) m/s 7 . 393 16 914 . 30 93 . 100 16 = + = v

( )
0 0
, y x
( ) x f
1
( )
1 1
, y x
x
y
Figure 3 Linear interpolation.
EXAMPLE 2:QUADRATIC
The upward velocity of a rocket is given as a
function of time in Table 2.
Find the velocity at t=16 seconds using the
direct method for quadratic interpolation.



16
0 0
10 227.04
15 362.78
20 517.35
22.5 602.97
30 901.67
Table 2 Velocity as a function
of time.
Figure 5 Velocity vs. time data for the
rocket example
( ) s , t ( ) ( ) m/s , t v
17
QUADRATIC INTERPOLATION


( )
2
2 1 0
t a t a a t v + + =

( )
0 0
, y x
( )
1 1
, y x
( )
2 2
, y x
( ) x f
2
y
x
Figure 6 Quadratic interpolation.
18
QUADRATIC INTERPOLATION


( )
2
2 1 0
t a t a a t v + + =
( ) ( ) ( ) 04 . 227 10 10 10
2
2 1 0
= + + = a a a v
( ) ( ) ( ) 78 . 362 15 15 15
2
2 1 0
= + + = a a a v
( ) ( ) ( ) 35 . 517 20 20 20
2
2 1 0
= + + = a a a v

( )
0 0
, y x
( )
1 1
, y x
( )
2 2
, y x
( ) x f
2
y
x
Figure 6 Quadratic interpolation.
19
Quadratic Interpolation


( )
2
2 1 0
t a t a a t v + + =
( ) ( ) ( ) 04 . 227 10 10 10
2
2 1 0
= + + = a a a v
( ) ( ) ( ) 78 . 362 15 15 15
2
2 1 0
= + + = a a a v
( ) ( ) ( ) 35 . 517 20 20 20
2
2 1 0
= + + = a a a v
Solving the above three equations gives
05 . 12
0
= a 733 . 17
1
= a
3766 . 0
2
= a

( )
0 0
, y x
( )
1 1
, y x
( )
2 2
, y x
( ) x f
2
y
x
Figure 6 Quadratic interpolation.
QUADRATIC INTERPOLATION (CONT.)
20
10 12 14 16 18 20
200
250
300
350
400
450
500
550
517.35
227.04
y
s
f range ( )
f x
desired ( )
20 10 x
s
range , x
desired
,
( ) 20 10 , 3766 . 0 733 . 17 05 . 12
2
s s + + = t t t t v
( ) ? 16 = v
QUADRATIC INTERPOLATION (CONT.)
21
10 12 14 16 18 20
200
250
300
350
400
450
500
550
517.35
227.04
y
s
f range ( )
f x
desired ( )
20 10 x
s
range , x
desired
,
( ) 20 10 , 3766 . 0 733 . 17 05 . 12
2
s s + + = t t t t v
( ) ( ) ( )
2
16 3766 . 0 16 733 . 17 05 . 12 16 + + = v
m/s 19 . 392 =
QUADRATIC INTERPOLATION (CONT.)
22
10 12 14 16 18 20
200
250
300
350
400
450
500
550
517.35
227.04
y
s
f range ( )
f x
desired ( )
20 10 x
s
range , x
desired
,
The absolute relative approximate error obtained between
the results from the first and second order polynomial is
% 38410 . 0
100
19 . 392
70 . 393 19 . 392
=

= e
a
EXAMPLE 3: CUBIC
The upward velocity of a rocket is given as a
function of time in Table 3.
Find the velocity at t=16 seconds using the
direct method for cubic interpolation.



23
0 0
10 227.04
15 362.78
20 517.35
22.5 602.97
30 901.67
Table 3 Velocity as a function
of time.
Figure 6 Velocity vs. time data for the
rocket example
( ) s , t ( ) ( ) m/s , t v
CUBIC INTERPOLATION
24




( )
3
3
2
2 1 0
t a t a t a a t v + + + =
y
x
( ) x f
3
( )
3 3
, y x
( )
2 2
, y x
( )
1 1
, y x
( )
0 0
, y x
Figure 7 Cubic interpolation.
CUBIC INTERPOLATION
25




( )
3
3
2
2 1 0
t a t a t a a t v + + + =
( ) ( ) ( ) ( )
3
3
2
2 1 0
10 10 10 04 . 227 10 a a a a v + + + = =
( ) ( ) ( ) ( )
3
3
2
2 1 0
15 15 15 78 . 362 15 a a a a v + + + = =
( ) ( ) ( ) ( )
3
3
2
2 1 0
20 20 20 35 . 517 20 a a a a v + + + = =
( ) ( ) ( ) ( )
3
3
2
2 1 0
5 . 22 5 . 22 5 . 22 97 . 602 5 . 22 a a a a v + + + = =
?
0
= a ?
1
= a ?
2
= a
?
3
= a
y
x
( ) x f
3
( )
3 3
, y x
( )
2 2
, y x
( )
1 1
, y x
( )
0 0
, y x
Figure 7 Cubic interpolation.
CUBIC INTERPOLATION
26




( )
3
3
2
2 1 0
t a t a t a a t v + + + =
( ) ( ) ( ) ( )
3
3
2
2 1 0
10 10 10 04 . 227 10 a a a a v + + + = =
( ) ( ) ( ) ( )
3
3
2
2 1 0
15 15 15 78 . 362 15 a a a a v + + + = =
( ) ( ) ( ) ( )
3
3
2
2 1 0
20 20 20 35 . 517 20 a a a a v + + + = =
( ) ( ) ( ) ( )
3
3
2
2 1 0
5 . 22 5 . 22 5 . 22 97 . 602 5 . 22 a a a a v + + + = =
2540 . 4
0
= a 266 . 21
1
= a 13204 . 0
2
= a
0054347 . 0
3
= a
y
x
( ) x f
3
( )
3 3
, y x
( )
2 2
, y x
( )
1 1
, y x
( )
0 0
, y x
Figure 7 Cubic interpolation.
CUBIC INTERPOLATION (CONTD)
10 12 14 16 18 20 22 24
200
300
400
500
600
700
602.97
227.04
y
s
f range ( )
f x
desired ( )
22.5 10 x
s
range , x
desired
,
27
( ) 5 . 22 10 , 0054347 . 0 13204 . 0 266 . 21 2540 . 4
3 2
s s + + + = t t t t t v
( ) ( ) ( ) ( )
m/s 06 . 392
16 0054347 . 0 16 13204 . 0 16 266 . 21 2540 . 4 16
3 2
=
+ + + = v
The absolute percentage relative
approximate error between
second and third order polynomial is
a
e
% 033269 . 0
100
06 . 392
19 . 392 06 . 392
=

= e
a
COMPARISON TABLE
Order of
Polynomial
1 2 3
( ) m/s 16 = t v 393.7 392.19 392.06
Absolute Relative
Approximate Error
---------- 0.38410 % 0.033269 %

28
Table 4 Comparison of different orders of the polynomial.
DISTANCE FROM VELOCITY PROFILE
Find the distance covered by the rocket from t=11s to t=16s ?
29
( ) 5 . 22 10 , 0054606 . 0 13064 . 0 289 . 21 3810 . 4
3 2
s s + + + = t t t t t v
( ) ( )
?
? 11 16
=
= s s
DISTANCE FROM VELOCITY PROFILE
Find the distance covered by the rocket from t=11s to t=16s ?
30
( ) 5 . 22 10 , 0054606 . 0 13064 . 0 289 . 21 3810 . 4
3 2
s s + + + = t t t t t v
( ) ( ) ( )
( )
m 1605
4
0054347 . 0
3
13204 . 0
2
266 . 21 2540 . 4
0054347 . 0 13204 . 0 266 . 21 2540 . 4
11 16
16
11
4 3 2
16
11
3 2
16
11
=
(

+ + + =
+ + + =
=
}
}
t t t
t
dt t t t
dt t v s s
ACCELERATION FROM VELOCITY PROFILE
31
( ) 5 . 22 10 , 0054347 . 0 13204 . 0 266 . 21 2540 . 4
3 2
s s + + + = t t t t v
Find the acceleration of the rocket at t=16s given that
( ) = t a

ACCELERATION FROM VELOCITY PROFILE
32
( ) 5 . 22 10 , 0054347 . 0 13204 . 0 266 . 21 2540 . 4
3 2
s s + + + = t t t t v
Find the acceleration of the rocket at t=16s given that
( ) ( )
( )
5 . 22 10 , ? ? ?
0054347 . 0 13204 . 0 266 . 21 2540 . 4
2
3 2
s s + + =
+ + + =
=
t t t
t t t
dt
d
t v
dt
d
t a

ACCELERATION FROM VELOCITY PROFILE
33
( ) ( )
( )
5 . 22 10 , 016382 . 0 26130 . 0 289 . 21
0054347 . 0 13204 . 0 266 . 21 2540 . 4
2
3 2
s s + + =
+ + + =
=
t t t
t t t
dt
d
t v
dt
d
t a

( ) ? 16 = a
ACCELERATION FROM VELOCITY PROFILE
34

( ) ( ) ( )
2
2
m/s 665 . 29
16 016304 . 0 16 26408 . 0 266 . 21 16
=
+ + = a

Vous aimerez peut-être aussi