View
0
Download
0
Category
Preview:
Citation preview
1 Scilab
1.1PDF Scilab
http://www.isee.nagoya-u.ac.jp/~shinimada/
1.2 ScilabScilab ( )
MATLAB ( )MATLAB Windows Scilab linux
Mac PC Scilab URLhttp://www.scilab.org/download/ (Scilab )
1.3 Scilab1.3.1
scilab-5.4.1 Scilab 1.1 Scilab
scilab scilab-5.4.1
1.1: Scilab
1.3.2Scilab Scilab (--> ) “exit” “File”
“Exit” ( Scilab )
1
1.4
--> help [ ]
( )
--> help plot // plot--> help if // if (if then else)
1.5
C Scilab
--> a=1 // a 1 a=1a =
1. // Scilab
--> a=1; // (;)
--> a=1, b=2; c=3; A=4 // (,) (;)a = // (a) (A)
1.A =
4. // (;)
--> a // aa =
1.
--> clear //
--> a // a!--error 4 // clear
undefined variable : a // a
1.6
--> 10+3*(16-2^3)/4ans =
16.
--> ans // ansans =
16.
--> a=1; b=2; c=3; A=4; d=a+b*c-Ad =
3.
1.1 . .* ./ .ˆ( ) 1.9
1.1:
a+b a-ba*b a/b aˆ b
a.*b a./b a.ˆ b
2
1.7
Scilab π i %
--> x = %pi // : %pix =
3.1415927
--> y = 3+2*%i // : %iy =
3. + 2.i--> real(y) // real yans =
3.--> imag(y) // imag yans =
2.
--> e = %eps // 1 + eps > 1 ( p.8)e = // %eps (1/2) 52 52 double
2.220E-16
--> 20-15;--> ans // : ans (ans % )ans =
5.
1.8 format
Scilab 8
--> x = 3.14159265358979323846264338327950288419716x =
3.1415927 // 8
format
--> format(14) // -2--> xx =
3.14159265359 // 12--> format(34)--> xx = 3.1415926535897931159979634685442 // 32 16
1.9
1.9.1
--> x = 0:10 // 0 10x =
0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
--> y = 1:3:10 // 1 10 3y =
1. 4. 7. 10.
--> a = y(3) // y 3 ( 1 )a =
7.
3
今田晋亮
--> a = y(5) // y 5!--error 21 // 5
invalid index
--> a = y(0) // y 0!--error 21 // 1 0
invalid index
--> b = y($) // yb =
10.
--> c = x(1:3) // x 3c =
0. 1. 2.
--> d = x(2:3:$) // x 2 3d =
1. 4. 7. 10.
--> z = y’ //z =
1.4.7.10.
--> y+y //ans =
2. 8. 14. 20.
--> sum(y) //ans =
22.
--> prod(y) //ans =
280.
--> y*y’ // y y ( )ans =
166.
--> y’*y // y y ( )ans =
1. 4. 7. 10.4. 16. 28. 40.7. 28. 49. 70.10. 40. 70. 100.
--> y.*y // y ( )ans =
1. 16. 49. 100.
4
1.9.2
--> A = [1,2,3;4,5,6] // 2 3 (;)A = // (,) A=[1 2 3; 4 5 6]
1. 2. 3.4. 5. 6.
--> A = [1,2,3 //--> 4,5,6]A =
1. 2. 3.4. 5. 6.
--> x = A(2,1) // 2 1 ( 1 )x =
4.
--> y = A(1,:) // 1y =
1. 2. 3.
--> z = A(:,2) // 2z =
2.5.
--> A’ // Aans =
1. 4.2. 5.3. 6.
--> B = eye(3,3) // 3 3B =
1. 0. 0.0. 1. 0.0. 0. 1.
--> C = zeros(2,3) // 2 3 0C =
0. 0. 0.0. 0. 0.
--> D = ones(2,3) // 2 3 1D =
1. 1. 1.1. 1. 1.
--> A.*A // Aans =
1. 4. 9.16. 25. 36.
--> A’*A // A Aans =
17. 22. 27.22. 29. 36.27. 36. 45.
--> 2*A //ans =
2. 4. 6.8. 10. 12.
5
1.2:
a < b a ≤ b a > b a ≥ b a = b a #= b and orScilab a=b a==b ã =b or ab &
--> C = [1,2;3,4] // 2x2 CA =
1. 2.3. 4.
--> C*C // C*C ( )ans =
7. 10.15. 22.
--> C.*C // C.*C ( )ans = // C*C C.*C
1. 4.9. 16.
1.10
1.10.1 if
if then // then
elseif then
else
end
1.2
--> score = 70;--> if score > 80 then--> disp("Well done.");--> elseif score > 60 then--> disp("Good.");--> else--> disp("Be serious.");--> endGood. // 2
1.10.2 for
for =
end
--> s = 0; // s=0--> for i = 1:10 // for i=[1 2 3 ... 10]--> s = s + i; // s i--> end--> s // ss =
55.
6
--> for k=0:2:4 // for k=[0 2 4]--> disp(k); // k--> end
0.2.4.
--> for k=3:-1:0 // for k=[3 2 1 0]--> disp(k); // k--> end
3.2.1.0.
1.10.3 while
while
end
--> a = 3;--> while a > 0--> disp(a);--> a=a-1;--> end
3.2.1.
1.10.4 select-case
C switch-case
--> n = ceil(4*rand()) // 0 4 1,2,3,4 n--> select n--> case 1 then--> disp("odd number!") // n = 1 odd number! ( )--> case 3 then--> disp("odd number!") // n = 3 odd number! ( )--> else--> disp("even number!") // n = 2 or 4 even number! ( )--> endn = // 2.xxxx n=3
3.odd number! // case 3 odd number!
7
1.11
Scilab( Scilab )
SciNotessample.sci
.sci
( )n-ana (
)
// --- sample.sci -----------a=10;b=20;x=a*b// ---------------------------------
Scilab “File” “Change Directory”“File”
“Exec...” ( sample.sci)“ ”
--> a=10;--> b=20;--> x=a*bx =
200.
exec
--> exec(’sample.sci’) // ’ ’ " "--> a=10;--> b=20;--> x=a*bx =
200.
1.12
Scilab // Scilab//
--> a=1 // a=2 a=2a =
1.
1.13
1.13.1 ( 1) 2 multiply ( 2 1 )
multiply.sci 2 Scilab multiply
// --- multiply.sci -------------function z = multiply(x,y) // 1 2 multiplyz = x*y // x yendfunction // (endfunction )// ---------------------------------
8
Scilab ScilabScilab “File” “Exec...”
( multiply.sci) “ ”multiply
--> multiply(10,5)*2ans =
100.
exec
--> exec(’multiply.sci’)--> multiply(10,5)*2ans =
100.
1.13.2 ( 2) circle ( 1 2 )
C ( ) Scilab
// --- circle.sci -----------function [perimeter, area] = circle(r) // 1 2 circleperimeter = 2 * %pi * r; // 1area = %pi * r * r; // 2endfunction //// ---------------------------------
circle
--> exec(’circle.sci’) // circle--> [p,a]=circle(3) // 3a = // ( :[p,a])
28.274334 // ap =
18.849556 // p (p a )
1.13.3 ( 3) 0,1 significand ( 1 1 )
1 x (52 ) 0,1
// --- significand.sci -----function bit = significand(x) // 0 exec("significand.sci") // significand--> significand(0.0328) // 0.0328ans =0000110010110010100101011110100111100001101100001001
9
−10 −8 −6 −4 −2 0 2 4 6 8 10−1.0
−0.8
−0.6
−0.4
−0.2
0.0
0.2
0.4
0.6
0.8
1.0
1.2: plot (sin(x))
−10 −8 −6 −4 −2 0 2 4 6 8 10−1.0
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1.0
−10 −8 −6 −4 −2 0 2 4 6 8 10−1.0
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1.0
1.3: plot2d (sin(x), cos(x))
−10 −8 −6 −4 −2 0 2 4 6 8 10−15
−10
−5
0
5
10
15
1.4: plot2d (sin(x)-x, 2cos(x))
1.14
--> x=-10:0.001:10; // x -10 10 0.001 . x=[-10 -9.999 -9.998 ... 9.999 10]--> y=sin(x); // y=[sin(-10) sin(-9.999) ... sin(9.999) sin(10)]--> plot(x,y); // sin(x) ( 1.2). plot(x,sin(x)) OK
--> mtlb_hold off // MATLAB hold (mtlb_ MATLAB )//// mtlb_hold on
--> y=sin(x);--> z=cos(x);--> plot2d(x’,[y’ z’]); // sin(x), cos(x) ( 1.3)
// 2 plot2d// plot2d (’ )
--> mtlb_hold off--> y=sin(x)-x;--> z=2*cos(x);--> plot2d(x’,[y’ z’]); // sin(x)-x, 2cos(x) ( 1.4)
1.15
Scilab File ExportOk
10
2
2.1
0 (unsigned)(signed) 2
2.1.1
C unsigned char , unsigned short , unsigned int , unsigned longunsigned char 1 (8 ) unsigned short 2 (16 ) unsigned int 4
(32 ) unsigned long 8 (64 ) 0 unsigned char8 0,1 2 00000000 11111111 (10 0 255) 256
2.1.2
C char , short , int , long char 1 (8 )short 2 (16 ) int 4 (32 ) long 8 (64 )
char −128 127 256 (1 )
2 2 20 1 1 char −100 2
100 = 64 + 32 + 4 = 26 + 25 + 22 2 1100100 0 8 011001000 1 10011011 1 10011100 −100 2
2.2
4 (32 ) 8 (64 )
(−1) × (1 + 0. )× 2( − )
2.2.1
(4 ) C float 1 823 32 (32 ) 0,1 01 8 −127 128 127 0255( 8 ) 127
2.2.3 0 ( 0) 255 ( 1)( ) 1 254 −126 127
(128 )10 −18.3125
−18.3125 = −(16 + 2 + 0.25 + 0.0625) = −(24 + 21 + 2−2 + 2−4) = −(10010.0101)2 = −(1.00100101)2 × 24
11
1, 24 4 127 131 2 100000111.00100101 1. 00100101 0 00100101000000000000000 32
0,1
11000001100100101000000000000000 ← −18.3125 float
−32768 float
−32768 = −215 = −(1.0)2 × 215
1 215 15 127 142 2 10001110, 1.0 1.0 23 0
11000111000000000000000000000000 ← −32768 float
−32768 int
00000000000000001000000000000000 (32768 2 )
↓ 0 111111111111111110111111111111111 (−32768 1 )
↓ 111111111111111111000000000000000 ← −32768 int (−32768 2 )
int float 4 (32 ) 0,1
2.2.2
(8 ) C double 1 1152 64 (64 ) 0,1 (4 ) 2
0 1
11 −1023 1024 1023 02047( 11 ) 1023
2.2.3 0 ( 0) 2047 ( 1)( ) 1 2046 −1022
1023 (1024 )
2.2.3
42.1
2.1:
zero Zero 0 1 00 · · · 0 00 · · · 0 ±0 2
denormal number, Denormal 0 1 00 · · · 0 #= 00 · · · 0 (float 2−149 ≤ |r| < 2−126 )subnormal number (double 2−1074 ≤ |r| < 2−1022 )
(infty) Inf 0 1 11 · · · 1 00 · · · 0 ±∞ 2 ( )
NaN 0 1 11 · · · 1 #= 00 · · · 0 NaN(not a number) ( )
12
3
π/4(1.8) (k = 0 1000000) π/4
24
3.1
3.1.1 (1.8) π/4 Scilab ”pi4.sci”
(Aitken.sci )
1 // ==================================================================2 // pi4.sci: (1.8) n Scilab3 // ==================================================================4 function Sn = pi4(n);5 Sn = 0;6 for k = n:-1:0 // k = 0:n OK. (n 0)7 Sn = Sn+((-1)^k)/(2*k+1)8 end9 endfunction
3.1.2 π/4 Scilab ”Aitken.sci”
1 // ----------------------------------------------------------------------2 // (1.8) /43 //4 //5 // S: (1.19) S_n^(k) (p.10 ) 26 // m: S (m = n + 1) n = 0,1,2, , m = 1,2,3,7 // h: S (h = k + 1) k = 0,1,2, , h = 1,2,3,8 //9 // Scilab 1 (C 0 )10 // (1.19) n,k 0 0 S11 // m,h S_n^(k) S(m,h)12 // ----------------------------------------------------------------------13 clear; // ( OK)14 stacksize(10000000); // ( )1516 itr = 1000000; // (1.8) ( )17 S_real = %pi/4; // (1.8) /4=0.785398163418 exec(’pi4.sci’); // (1.8) pi4.sci19 S_slow = pi4(itr); // (1.8)20 err1 = abs(S_real - S_slow); // (1.8)21 // ( )22 for n=0:itr23 m = n + 1; // m S (n 0 m 1 )24 S(m,1) = pi4(n); // p.9 S_n^(0)25 err2 = abs(S(m,1) - S_real);26 for k = 0:((n/2)-1) // p.927 if err2
31 m = m - 1; //32 S(m,h+1) = (S(m+1,h)*S(m-1,h)-S(m,h)^2)/(S(m+1,h)+S(m-1,h)-2*S(m,h));33 err2 = abs(S(m,h+1) - S_real); // (1.19)34 end35 if err2 exec("Aitken.sci"); // exec! 1. 0. 0. 0. !! 0.66666667 0.79166667 0. 0. !! 0.86666667 0.78333333 0.78552632 0. !! 0.72380952 0.78630952 0.78536255 0.78539984 !! 0.83492063 0.78492063 0.78541083 0.78539772 !! 0.74401154 0.78567821 0.78539282 0.78539831 !! 0.82093462 0.78522034 0.78540071 0. !! 0.75426795 0.78551795 0. 0. !! 0.81309148 0. 0. 0. !
error1 = 0.0000002499997500error2 = 0.0000001444154701S_real = 0.7853981633974483S_slow = 0.7853984133971983S(5,3) = 0.7853983078129184 // n = 5, k = 3 ( 24 )
14
4
4.1 ( ) Scilab ”Gauss pivot.sci”
( ) ( p.93)
1 // =============================================================================2 // Gauss_pivot.sci: ( )3 // -----------------------------------------------------------------------------4 // : A : n n5 // b : n 16 // : x : n 17 // =============================================================================8 function x = Gauss_pivot(A, b)9 [n, n1] = size(A); // (n n1)10 for i = 1:n-1 // 1 n-111 [m,k] = max(abs(A(i:n,i))); // i i12 // k ( i+k-1 )13 if k > 1 //14 tmp1 = A(i,:); // A i tmp115 tmp2 = b(i); // b i tmp216 A(i,:) = A(i+k-1,:); // A i i+k-117 b(i) = b(i+k-1); // b i i+k-118 A(i+k-1,:) = tmp1; // A i+k-1 i19 b(i+k-1) = tmp2; // b i+k-1 i20 end2122 for h = i+1:n // i+123 r = A(h,i)/A(i,i); // h24 A(h,:) = A(h,:) - r * A(i,:); // A h (i 0 )25 b(h) = b(h) - r * b(i); // b h26 end;27 end; //2829 x = zeros(n,1); // x 030 x(n) = b(n) / A(n,n); // (n ) n x(n)3132 for i = n-1:-1:1 //33 x(i) = (b(i) - A(i,i+1:n) * x(i+1:n,1)) / A(i,i);34 end // i i x(i)35 endfunction
--> exec("Gauss_pivot.sci");--> A = [1 100 3; 1 3 4; 50 1 3];--> b = [4 3 2]’--> x = Gauss_pivot(A,b) // Gauss_pivot Ax=bx =- 0.004621460.017914630.73771939
--> x = A\b // Gauss_pivot \ Ax=b xx =- 0.004621460.017914630.73771939
15
4.2
A b
5x1 −x2 = 9−x1 +5x2 −x3 = 4
−x2 +5x3 = −6A =
5 −1 0−1 5 −10 −1 5
, x =
x1
x2
x3
, b =
9
4
−6
x(0) = [0 0 0]′
4.2.1
x1 = x2/5 +9/5
x2 = x1/5 +x3/5 +4/5
x3 = x2/5 − 6/5
x1
x2
x3
=
0 1/5 0
1/5 0 1/5
0 1/5 0
x1
x2
x3
+
9/5
4/5
−6/5
C =
0 1/5 0
1/5 0 1/5
0 1/5 0
, r =
9/5
4/5
−6/5
x(n) = Cx(n−1) + r
x(n) n x(n)
x(0) =
0
0
0
x(1) =
0 1/5 0
1/5 0 1/5
0 1/5 0
0
0
0
+
9/5
4/5
−6/5
=
9/5
4/5
−6/5
=
1.8
0.8
−1.2
x(2) =
0 1/5 0
1/5 0 1/5
0 1/5 0
9/5
4/5
−6/5
+
9/5
4/5
−6/5
=
49/25
23/25
−24/25
=
1.96
0.92
−0.96
x(3) =
0 1/5 0
1/5 0 1/5
0 1/5 0
49/25
23/25
−24/25
+
9/5
4/5
−6/5
=
248/125
1
−128/125
=
1.984
1
−1.024
x(4) =
0 1/5 0
1/5 0 1/5
0 1/5 0
248/125
1
−128/125
+
9/5
4/5
−6/5
=
2
124/125
−1
=
2
0.992
−1
...
x = [ 2 1 − 1 ]′
16
4.2.2
C =
c1
c2
c3
=
0 1/5 0
1/5 0 1/5
0 1/5 0
, r =
r1
r2
r3
=
9/5
4/5
−6/5
n x(n) =[x(n)1 x
(n)2 x
(n)3
]′
x(n)1 = c1[x(n−1)1 x
(n−1)2 x
(n−1)3
]′+ r1
x(n)2 = c2[x(n)1 x
(n−1)2 x
(n−1)3
]′+ r2
x(n)3 = c3[x(n)1 x
(n)2 x
(n−1)3
]′+ r3
x(0) = [ 0 0 0 ]′
x(1)1 = [ 0 1/5 0 ] [ 0 0 0 ]′ + 9/5 = 9/5 = 1.8
x(1)2 = [ 1/5 0 1/5 ] [ 9/5 0 0 ]′ + 4/5 = 29/25 = 1.16
x(1)3 = [ 0 1/5 0 ] [ 9/5 29/25 0 ]′ − 6/5 = −121/125 = −0.968
x(2)1 = [ 0 1/5 0 ] [ 9/5 29/25 − 121/125 ]′ + 9/5 = 254/125 = 2.032x(2)2 = [ 1/5 0 1/5 ] [ 254/125 29/25 − 121/125 ]′ + 4/5 = 633/625 = 1.0128x(2)3 = [ 0 1/5 0 ] [ 254/125 633/625 − 121/125 ]′ − 6/5 = −3117/3125 = −0.99744
x(3)1 = [ 0 1/5 0 ] [ 254/125 633/625 − 3117/3125 ]′ + 9/5 = 6258/3125 = 2.00256x(3)2 = [ 1/5 0 1/5 ] [ 6258/3125 633/625 − 3117/3125 ]′ + 4/5 = 15641/15625 = 1.001024x(3)3 = [ 0 1/5 0 ] [ 6258/3125 15641/15625 − 3117/3125 ]′ − 6/5 = −78109/78125 = −0.9997952
...
x = [ 2 1 − 1 ]′
4.3 Scilab
4.3.1 Scilab ”Seidel.sci”
1 // =============================================================================2 // Seidel.sci: Ax=b3 // -----------------------------------------------------------------------------4 // A: A (n n)5 // b: b (n 1)6 // x0: (n 1)7 // tol: 2 ( )8 // itr:9 // x: x (n 1)10 // =============================================================================11 // C: (5.41) a_ij/a_ii ( 1,2 )12 // r: (5.41) b_i/a_ii ( 3 )13 // S:1415 function x = Seidel(A, b, x0, tol, itr)16 [n, m] = size(A); //17 C = -A; // C = -A ( A )18 for i = 1:n19 C(i,i) = 0; // C 0
17
20 C(i,1:n) = C(i,1:n) / A(i,i); // C A21 r(i) = b(i) / A(i,i); // b A22 end2324 S = x0; // S25 x = x0; // x26 for k = 1:itr // itr27 for i = 1:n // i=1,2, i x(i)28 x(i) = C(i,:) * x + r(i); // x(i) x(i+1)29 end30 S(:,k+1) = x; // k S31 if norm(S(:,k) - x)^2 < tol // 232 break;33 end34 end35 format(7); // 536 disp(S’); // (S )3738 endfunction
4.3.2 Seidel 5 Scilab ”ex5 Seidel.sci”
1 // -----------------------------------------------------------2 // Seidel ( 5 Ax=b : 5.2 )3 // -----------------------------------------------------------4 clear; //5 exec(’Seidel.sci’); // Seidel6 A = [ 10, 3, 1, 2, 1; // Ax=b A7 1,19, 2,-1, 5;8 -1, 1,30, 1,10;9 -2, 0, 1,20, 5;10 -3, 5, 1,-2,25 ];11 b = [-22, 27, 89,-73, 22]’; // Ax=b b12 x0 = [ 0, 0, 0, 0, 0]’; //13 tol = 1.0e-8; // x 214 itr = 100; //15 x = Seidel( A, b, x0, tol, itr ) // Seidel16 // (;) x
4.3.3 Scilab ”ex5 Seidel.sci”
--> exec(’ex5_Seidel.sci’);0. 0. 0. 0. 0. // x
- 2.2 1.5368 2.8421 - 4.0121 - 0.1260 // Seidel disp(S’)- 2.1302 1.056 3.0362 - 3.9833 - 0.0269 // 5.2- 2.0211 1.0053 3.0075 - 3.9957 - 0.0035- 2.0028 1.0005 3.0009 - 3.9994 - 0.0004- 2.0003 1.0001 3.0001 - 3.9999 - 5.D-05- 2. 1. 3. - 4. - 6.D-06- 2. 1. 3. - 4. - 7.D-07
x = // x- 2. // ( )1.3.
- 4.- 7.D-07
18
5 ( )
5.1 Scilab1 Scilab poly, horner, derivat y
z
y = (x− 1)(x− 2)
= x2 − 3x+ 2
z =x2
(x− 1)(x− 5)(x+ 3)
=x2
x3 − 3x2 − 13x+ 15
5.1.1 poly
(roots)
y = (x− 1)(x− 2) poly 1[1 2] 2 ( ’x’) 3 ’roots’
3 ’roots’
--> y = poly([1 2], ’x’) // y ( )y = // y = poly([1, 2], ’x’, ’roots’) (’roots’ )
22 - 3x + x
--> roots(y) // rootsans =
1. // 1 2 y2.
--> z = poly([0 0],’x’) / poly([1 5 -3],’x’) // z ( )z =
2x
-----------------2 3
15 - 13x - 3x + x
--> s = y^2 - 1 // y^2 - 1 = {(x-1)(x-2)}^2 - 1s =
2 3 43 - 12x + 13x - 6x + x
--> y * z // y zans =
2 3- 2x + x----------
2- 15 - 2x + x
1
19
(coefficients)
y = x2 − 3x+ 2 1[2 − 3 1], 2 ( ’x’) 3 ’coeff’
--> y = poly([2 -3 1],’x’,’coeff’) // y ( )y =
22 - 3x + x
--> coeff(y) // y coeffans =
2. - 3. 1.
--> z = poly([0 0 1],’x’,’coeff’) / poly([15 -13 -3 1],’x’,’coeff’) // zz =
2x
-----------------2 3
15 - 13x - 3x + x
--> coeff(numer(z)) // z numerans =
0. 0. 1.
--> coeff(denom(z)) // z denomans =
15. - 13. - 3. 1.
--> x = poly(0, ’x’); // x--> y = x^2 - 3*x + 2 // yy =
22 - 3x + x
--> x = poly(0, ’x’); // x--> z = x^2 / (x^3-3*x^2-13*x+15) // zz =
2x
-----------------2 3
15 - 13x - 3x + x
5.1.2 horner
horner( )
--> horner(y, 5) // y = x^2-3x+2 x 5ans =
12.
--> horner(z,-2) // z = x^2/(x^3-3*x^2-13*x+15) x -2ans =
0.1904762
20
今田晋亮
今田晋亮
5.1.3 derivat
derivat
--> dy = (derivat(y)) // ydy =- 3 + 2x
--> ddy = derivat(dy) // y 2ddy =
2
--> dz = derivat(z) // zdz =
2 430x - 13x - x
-------------------------------------2 3 4 5 6
225 - 390x + 79x + 108x - 17x - 6x + x
5.2 deff feval1 exec
defffeval horner feval
--> deff(’y = f(x)’,’y = x*cos(x)’); // f--> f(2) // f 2ans =- 0.8322937
--> feval(2, f) // f(2)ans =- 0.8322937
--> deff(’y = f(x)’,’y = x^2-3*x+2’); // f--> a = f(5) // f 5ans =
12.--> a = feval(5, f) // f(5)a =
12.
5.35.3.1 ”Lagrange.sci”
1 // ==================================================================2 // Lagrange.sci:3 // ------------------------------------------------------------------4 // x: x5 // y: y6 // f:7 // ==================================================================8 function f = Lagrange(x, y)9 f = 0; // f 010 for i = 1:length(x) // length(x) x ( )11 xi = [x(1:i-1), x(i+1:$)]; // xi x i12 f = f + poly(xi, ’x’) * y(i) / prod( x(i) - xi ); // p.17 (2.11)13 end14 endfunction
21
5.3.2 Lagrange 1 (p.20) ”ex1 Lagrange.sci”
1 // --------------------------------------------------------------------------------2 // p.20 13 // sin(x) 5 44 // sin(x)5 // --------------------------------------------------------------------------------6 clear; //7 exec(’Lagrange.sci’); // Lagrange89 x = [0, %pi/4, %pi/2, 3*%pi/4, %pi]; // x10 y = [0, sqrt(2)/2, 1, sqrt(2)/2, 0]; // y11 f = Lagrange(x, y) // Lagrange1213 xx = x(1):0.001:x($); // 0 0.001 ( x )14 yy = horner(f, xx); // x xx (xx )15 y_real = sin(xx); // x xx sin(x) (xx )16 plot2d(xx’, [yy’, y_real’]); // sin(x) ( )
5.3.3 ”ex1 Lagrange.sci”
--> exec(’ex1_Lagrange.sci’);f =
2 3 40.9819684x + 0.0582877x - 0.2360955x + 0.0375758x // 4
0 0.4 0.8 1.2 1.6 2.0 2.4 2.8 3.20
0.2
0.4
0.6
0.8
1.0
1.2
5.1: 1 (sin Lagrange ) ”ex1 Lagrange.sci”
5.3.4 Lagrange 2 (p.24) ”ex2 Lagrange.sci”
1 // --------------------------------------------------------------------------------2 // p.24 23 // 4 3 3 204 // --------------------------------------------------------------------------------5 clear; //6 exec(’Lagrange.sci’); // Lagrange78 x = [19.9466, 19.9907, 20.0349, 20.0793]; // x9 y = [ 2.712, 2.714, 2.716, 2.718]; // y10 f = Lagrange(x, y) // Lagrange f11 yy = horner(f, 20); // f 2012 printf("f(20) = %f\n", yy); // 3 20 f(20)
5.3.5 ”ex2 Lagrange.sci”
--> exec("ex2_Lagrange.sci");f =
2 369.883617 - 10.193976x + 0.5133640x - 0.0085794x // 3
f(20) = 2.714421 // 3 20
22
6
6.1
6.1.1 ”trapezoid.sci”
1 // ===================================================================2 // trapezoid.sci:3 // -------------------------------------------------------------------4 // f:5 // a:6 // b:7 // N: [a,b]8 // I:9 // ===================================================================10 function I = trapezoid(f, a, b, N)11 h = (b - a) / N; //12 x = a:h:b; // x13 y = f(x); // y14 I = h * (y(1) + 2 * sum(y(2:N)) + y(N+1)) / 2; // (3.4)15 endfunction
6.1.2 (3.6) ”eq36 trapezoid.sci”
1 clear;2 exec(’trapezoid.sci’); //3 deff(’y=f(x)’,’y=sin(x)’); // f f(x)=sin(x)4 a = 0; //5 b = %pi/2; //6 N = 8; // ( )7 format(11); // 98 I = trapezoid(f,a,b,N) // (3.6)
6.1.3 ”eq36 trapezoid.sci”
--> exec(’eq36_trapezoid.sci’);I =
0.99678517 //
6.1.4 Scilab
Scilab inttrapinttrap ( help inttrap )
--> h = %pi/16; // h = /16--> x = 0:h:%pi/2; // 0 /2 /16--> I = inttrap(x, sin(x)) //I =
0.99678517 //
--> h = %pi/16;--> x = 0:h:%pi/2;--> deff(’y=f(x)’,’y=sin(x)’); // deff f(x)--> I = inttrap(x, f(x)) // 2 f(x)I =
0.99678517
23
6.2 ( )
6.2.1 ”GaussLegendre.sci”
1 // ==================================================================2 // GaussLegendre.sci:3 // ------------------------------------------------------------------4 // f:5 // a:6 // b:7 // N: ( N 2,3,4,5 )8 // I:9 // ==================================================================10 function I = GaussLegendre(f, a, b, N)11 xi = [-sqrt(1/3) -sqrt(3/5) -0.8611363116 -0.9061798459 // p.53 3.312 sqrt(1/3) 0 0.3399810436 -0.538469310113 0 sqrt(3/5) 0.3399810436 014 0 0 0.8611363116 0.538469310115 0 0 0 0.9061798459];1617 wi = [ 1 5/9 0.3478548451 0.2369268851 // p.53 3.318 1 8/9 0.6521451549 0.478628670519 0 5/9 0.6521451549 0.568888888920 0 0 0.3478548451 0.478628670521 0 0 0 0.2369268851];2223 x = xi(1:N, N-1); // [1,-1] xi24 x = ((b - a) * x + b + a) / 2; // [a,b] p.5125 y = f(x); // y = [f(x_1), f(x_2),..., f(x_N)]26 w = wi(1:N, N-1); // wi27 I = (b - a) / 2 * (w’ * y); // (3.20) (b-a)/2 [a,b]28 endfunction
6.2.2 3.4 ”tab34 GaussLegendre.sci”
1 clear;2 exec(’GaussLegendre.sci’); //3 deff(’y=f(x)’,’y=(x.*(1-x)).^(3/2)’); // f f(x)= {x(1-x)}^(3/2)4 a = 0; //5 b = 1; //6 for N = 2 : 5 // N = 2,3,4,57 S = (128/3) * GaussLegendre(f,a,b,N); //8 disp(S); // (3.4) 4 (N=2 5)9 end
6.2.3 ”tab34 GaussLegendre.sci”
--> exec(’tab34_GaussLegendre.sci’);
2.903099 // N = 2 (p.54 3.4 N=2 5 )3.1199473 // N = 33.1365993 // N = 43.1399284 // N = 5
6.2.4 Scilab
Scilab integrate integrate( help integrate )
--> deff(’y=f(x)’,’y=(x.*(1-x)).^(3/2)’);--> S = (128/3) * integrate(’f(x)’, ’x’, 0, 1)S =
3.1415927 // p.54 3.4 N = 100
24
今田晋亮
今田晋亮
7
7.1 ( )
7.1.1 ”Newton.sci”
1 // ==========================================================================2 // Newton.sci: ( p.72 (4.16))3 // --------------------------------------------------------------------------4 // f: f(x), df: f’(x), x0:5 // tol: , itr:6 // ==========================================================================7 function x = Newton(f, df, x0, tol, itr)8 x(1) = x0; // x x0 x(1)9 y(1) = f( x(1) ); // x0 f(x0) y(1)10 dy(1) = df( x(1) ); // x0 f’(x0) dy(1)1112 for k = 1:itr // itr13 x(k+1) = x(k) - y(k) / dy(k); // (4.16) x14 y(k+1) = f( x(k+1) ); // x f(x)15 dy(k+1) = df( x(k+1) ); // x f’(x)16 if ( abs( y(k+1) ) < tol ) then // (4.17)17 break; // for18 end19 end20 format(12) // 1021 disp([ [0:k]’, x, y ]) // x, y22 endfunction
7.1.2 p.72 2 ”ex2 Newton.sci”
f(x) = x3 +6x2 +21x+32 = 0
1 clear; //2 deff(’y=f(x)’,’y=x^3+6*x^2+21*x+32’); // f(x)3 deff(’y=df(x)’,’y=3*x^2+12*x+21’); // f’(x)4 exec(’Newton.sci’); //5 Newton(f,df,0,1.0e-10,20); // 0 1.0e-10, 20
7.1.3 ”ex2 Newton.sci”
--> exec(’ex2_Newton.sci’);0. 0. 32. // x = 0, y = f(0) = 321. - 1.523809524 10.39369399 // 12. - 2.597508059 0.409107601 // 23. - 2.638130209 - 0.003024983 // 34. - 2.637834269 - 0.000000168 // 45. - 2.637834253 3.55271E-15 // 5 . x = -2.637834253
7.2 Scilab rootsf(x) = 0 Scilab roots roots
--> y = poly([32,21,6,1],’x’,’coeff’); // f(x)=x^3+6x^2+21x+32--> roots(y) // f(x)=0 rootsans =- 2.637834253 // 1 ( )- 1.681082874 + 3.050430199i // 2 ( )- 1.681082874 - 3.050430199i // 3 ( )
25
今田晋亮
8
8.1 4
8.1.1 4 ”RungeKutta.sci”
1 // ==================================================================2 // RungeKutta.sci: 4 (6.42)3 // ------------------------------------------------------------------4 // f: f(x,y)5 // a,b: [a,b]6 // y0:7 // N:8 // x,y: (x: , y: )9 // ==================================================================10 function [x,y] = RungeKutta(f, a, b, y0, N)11 h = (b - a)/N; // h12 x = a:h:b; // x ( )13 y = y0 ; // y=y014 for i = 1 : N // N15 k1 = h * f( x(i), y(i) ); // (6.42) k116 k2 = h * f( x(i) + h/2, y(i) + k1/2 ); // (6.42) k217 k3 = h * f( x(i) + h/2, y(i) + k2/2 ); // (6.42) k318 k4 = h * f( x(i) + h, y(i) + k3 ); // (6.42) k419 y(i+1) = y(i) + (k1 + 2*k2 + 2*k3 + k4) / 6; // (6.42) y(i+1)20 end21 endfunction
8.1.2 4 p.141 3 ”ex3 RungeKutta.sci”
1 // --------------------------------------------------------------------------2 // RungeKutta 3 (p.141)3 // --------------------------------------------------------------------------4 clear; //5 exec(’RungeKutta.sci’); //6 deff(’dy=f(x,y)’,’dy=y-y^2’); // 37 a = 0; //8 b = 20; //9 y0 = 0.1; //10 N = 8; [x,y] = RungeKutta(f,a,b,y0,N); plot2d(x,y,1); // N = 811 N = 10; [x,y] = RungeKutta(f,a,b,y0,N); plot2d(x,y,2); // N = 1012 N = 20; [x,y] = RungeKutta(f,a,b,y0,N); plot2d(x,y,3); // N = 20
exec(’ex3 RungeKutta.sci’) 8.1
8.1: 3 (p.141) 4 (N = 8, 10, 20)
26
8.2 Scilab odeScilab (ODE; Ordinary Differential Equation) ode
ode 1 n 1
8.2.1 ode 1
ode
y′ = f(x, y), : y(x0) = y0 (8.1)
1 y′ = x+ y, y(0) = 0
f(x, y) = x+ y, x0 = 0, y0 = 0
y′ = x+ y, y(0) = 0 (8.2)
ode 4 (1) y0 (2) x0 (3) x x0 xN(4)
--> deff(’yd=f(x,y)’,’yd=x+y’); // f(x,y)=x+y--> x0=0; y0=0; // y(0)=0--> x=x0:0.2:1; // x=[0,1] 0.2 (x=[0,0.2,0.4,..,1])--> y=ode(y0,x0,x,f) // odey =0. 0.0214028 0.0918247 0.2221188 0.4255409 0.7182819 // ode
--> ye = exp(x)-x-1ye =0. 0.0214028 0.0918247 0.2221188 0.4255409 0.7182818 // exp(x)-x-1
1 y′ = x+ y, y(0) = 2 ( )
x0 = 0, y0 = 2
y′ = x+ y, y(0) = 2 (8.3)
ode
--> deff(’yd=f(x,y)’,’yd=x+y’); // f(x,y)=x+y--> x0=0; y0=2; // y(0)=2 ( )--> x=x0:0.2:1; // x=[0,1] 0.2--> y=ode(y0,x0,x,f) // odey =
2. 2.4642082 3.0754742 3.8663565 4.876623 6.1548466 // ode--> ye = 3*exp(x)-x-1ye =
2. 2.4642083 3.0754741 3.8663564 4.8766228 6.1548455 // 3*exp(x)-x-1
8.2 y′ = x+ y (y )
8.2: 1 y′ = x+ y 2 y(0) = 0, y(0) = 2
27
8.2.2 ode
n ( )
y(n) = f(x, y, y′, y′′, . . . , y(n−1)
), (8.4)
: y(x0) = α0, y′(x0) = α1, y
′′(x0) = α2, . . . , y(n−1)(x0) = αn−1
2 (y′′ = −0.3y′ − sin(y), y(0) = π/2, y′(0) = 0 )
ode 2 (n = 2)
y′′ = −0.3y′ − sin(y), (8.5): y(0) = π/2, y′(0) = 0
n ode (1) [α0,α2, . . . ,αn−1]′ (2) x0(3) x x0 xN (4)
u = y, v = y′
--> deff(’ydot=f(x,y)’, [’du=y(2)’, ’dv=-0.3*y(2)-sin(y(1))’, ’ydot=[du;dv]’])// u’=y’=f(x,u,v), v’=y"=g(x,u,v)
--> x0=0; y0=[%pi/2;0]; //--> x=0:0.1:15; // x=[0,15] 0.1--> y=ode(y0,x0,x,f); // ode--> plot2d(x,y(1,:)); // y--> plot2d(x,y(2,:)); // y’ ( 8.3)
ode y, y′ 8.3 n (n > 3) odeode y0 deff ydot n
8.2.3 ode 1
1
1 ( )
y1 y2
y′1 = y1 − y1y2 −1
10y21 (8.6)
y′2 = y1y2 − y2 −1
20y22 (8.7)
y1(0) = 2, y2(0) = 1 (8.8)
ode
--> deff(’ydot=f(x,y)’, [’dy1=y(1)-y(1)*y(2)-y(1)^2/10’, ’dy2=y(1)*y(2)-y(2)-y(2)^2/20’,’ydot=[dy1;dy2]’]); // y1’, y2’
--> x0=0; y0=[2;1]; //--> x=0:0.01:50; // x--> y=ode(y0,x0,x,f); //--> plot(y(1,:),y(2,:)); // y1, y2 ( 8.4)
8.4 (8.8)
28
8.3: 2 8.4: 8.5: shooting.sci
8.3 ( )
8.3.1 y′′ = p(x)y′ + q(x)y + r(x), y(a) = ya, y(b) = yb
2 ( )
y′′ =2x
x2 + 1y′ − 2
x2 + 1y + x2 + 1, y(0) = 2, y(1) =
5
3
y =x4
6− 3
2x2 + x+
x2
2
”f.sci”
1 // --------------------------------------------------------------------------2 //3 // --------------------------------------------------------------------------4 function dz = f(x,z)5 dz = [ z(2) // z(1)’= u’= z(2)6 2 * x./(x^2 + 1).*z(2) - 2/(x^2 + 1).*z(1) + x^2 + 1 // z(2)’= p(x)u’+q(x)u+r(x)7 z(4) // z(3)’= v’= z(4)8 2 * x./(x^2 + 1).*z(4) - 2/(x^2 + 1).*z(3) ]; // z(4)’= p(x)v’+q(x)v9 endfunction
2 ”shooting.sci”
ode
1 // --------------------------------------------------------------------------2 //3 // --------------------------------------------------------------------------4 clear; //5 exec(’f.sci’) //6 x = 0:0.001:1; // x7 n = length(x); // x8 a = 0; b = 1; // [a,b]=[0,1]9 ya = 2; yb = 5/3; // x=a,b10 z0 = [ya,0,0,1]’; //11 z = ode(z0, a, x, f); // ode1213 y = z(1,:) + (yb - z(1,n)) * z(3,:)./ z(3,n); // : y(x) = u(x)+(yb-u(b))/v(b)*v(x)14 plot2d(x,y); xgrid; //
exec(’shooting.sci’) 8.5
29
Recommended