Lecture 1: Introduction To Scilab: Kebaier@
Lecture 1: Introduction To Scilab: Kebaier@
Ahmed Kebaier
[email protected]
HEC, Paris
First Steps with Scilab Vector and Matrix
Outline
Outline
following instructions :
function d = carre(x)
d= x.*x
endfunction
2 Under Scilab load and execute the file carre.sci :
Exercise
Plot the cos function on the interval [−π, π]. Create a mesh of 11
points.
First Steps with Scilab Vector and Matrix
Exercise
Plot the cos function on the interval [−π, π]. Create a mesh of 11
points.
Outline
Exercise
give a value to the variable n then define the n × n matrix :
2 −1 0 . . . 0
−1 . . . . . . . . .
..
.
A = 0 ... ... ... 0
..
.. .. ..
. . . −1
.
0 . . . 0 −1 2
Solution
n = 7;
v = -ones(1,n-1);
A = diag(v,-1) + 2*eye(n,n) + diag(v,1)
First Steps with Scilab Vector and Matrix
Solution
n = 7;
v = -ones(1,n-1);
A = diag(v,-1) + 2*eye(n,n) + diag(v,1)
// another solution
// A = diag(v,-1) + diag(2*ones(1,n)) + diag(v,1)
First Steps with Scilab Vector and Matrix
Solution
n = 7;
v = -ones(1,n-1);
A = diag(v,-1) + 2*eye(n,n) + diag(v,1)
// another solution
// A = diag(v,-1) + diag(2*ones(1,n)) + diag(v,1)
b = rand(n,1);
x = A\ b
res = norm(A*x-b)/norm(b)
E=0.5*x’*A*x - b’*x
First Steps with Scilab Vector and Matrix
Solution
n = 7;
v = -ones(1,n-1);
A = diag(v,-1) + 2*eye(n,n) + diag(v,1)
// another solution
// A = diag(v,-1) + diag(2*ones(1,n)) + diag(v,1)
b = rand(n,1);
x = A\ b
res = norm(A*x-b)/norm(b)
E=0.5*x’*A*x - b’*x
y=rand(n,1);
F=0.5*y’*A*y - b’*y
First Steps with Scilab Vector and Matrix
Solution
n = 7;
v = -ones(1,n-1);
A = diag(v,-1) + 2*eye(n,n) + diag(v,1)
// another solution
// A = diag(v,-1) + diag(2*ones(1,n)) + diag(v,1)
b = rand(n,1);
x = A\ b
res = norm(A*x-b)/norm(b)
E=0.5*x’*A*x - b’*x
y=rand(n,1);
F=0.5*y’*A*y - b’*y
E<F
First Steps with Scilab Vector and Matrix
Solution
n = 7;
v = -ones(1,n-1);
A = diag(v,-1) + 2*eye(n,n) + diag(v,1)
// another solution
// A = diag(v,-1) + diag(2*ones(1,n)) + diag(v,1)
b = rand(n,1);
x = A\ b
res = norm(A*x-b)/norm(b)
E=0.5*x’*A*x - b’*x
y=rand(n,1);
F=0.5*y’*A*y - b’*y
E<F
B = [ A , eye(n,n) ;... eye(n,n), A ]
First Steps with Scilab Vector and Matrix
Exercise 3
Solution
n=5;
v=-ones(1,n-1);
A=diag(v,-1) + 2*eye(n,n) + diag(v,1)
B=[ A , eye(n,n) ;... eye(n,n), A ]
First Steps with Scilab Vector and Matrix
Solution
n=5;
v=-ones(1,n-1);
A=diag(v,-1) + 2*eye(n,n) + diag(v,1)
B=[ A , eye(n,n) ;... eye(n,n), A ]
C = A(:,n:-1:1)
First Steps with Scilab Vector and Matrix
Solution
n=5;
v=-ones(1,n-1);
A=diag(v,-1) + 2*eye(n,n) + diag(v,1)
B=[ A , eye(n,n) ;... eye(n,n), A ]
C = A(:,n:-1:1)
D = A(1:2:n,:)
First Steps with Scilab Vector and Matrix
Solution
n=5;
v=-ones(1,n-1);
A=diag(v,-1) + 2*eye(n,n) + diag(v,1)
B=[ A , eye(n,n) ;... eye(n,n), A ]
C = A(:,n:-1:1)
D = A(1:2:n,:)
E = B(n-2:n+3,n-2:n+3)
First Steps with Scilab Vector and Matrix
Programming tools I
Programming tools II
if tests
They permit to execute different blocks of code depending on
boolean expressions:
if bool expression then
// block executed when bool expression is TRUE
.....
else
// block executed when bool expression is FALSE
.....
end
Example
x = rand()
if x < 0.5 then
y = -1;
else
y = 1;
First Steps with Scilab Vector and Matrix
Programming tools IV
while loop
A while loop allows to repeat a block of code while a boolean
expression is true:
Programming tools IV
while loop
A while loop allows to repeat a block of code while a boolean
expression is true:
Try:
x = 1;
while x < 1000, x = 2*x, end
First Steps with Scilab Vector and Matrix
Programming tools IV
while loop
A while loop allows to repeat a block of code while a boolean
expression is true:
Try:
x = 1;
while x < 1000, x = 2*x, end