Programming With SCILAB
Programming With SCILAB
▼ 2012 (7)
Plot Heart Curve in Scilab ▼ April (6)
Plot Heart Curve in Scilab
The heart curve is a closed curve, which has the shape of a heart. Scilab can generate a heart
shape and with the its core functions. Recursive function
implementation in Scilab
Tips to Write XCOS program: Use
of GOTO/FROM block...
CSV File Handling
Perceptron using Scilab
Newton-Raphson Method to find
the roots of a polyn...
► January (1)
Scilab Script:
Example 1
Example 2
Output:
programming-with-scilab.blogspot.com 1/14
7/26/2019 Programming with Scilab
Output of Example 1
programming-with-scilab.blogspot.com 2/14
7/26/2019 Programming with Scilab
Output of Example 2
Labels: Example 1
Most computer programming languages support recursion by allowing a function to call itself within
the program text. Some functional programming languages do not define any looping constructs but
rely solely on recursion to repeatedly call code. Recursive function can solve the same kinds of
problems even without the typical control structures like “while” and “for”.
A well known mathematical recursive function is one that computes the Fibonacci numbers
Scilab implementation:
programming-with-scilab.blogspot.com 3/14
7/26/2019 Programming with Scilab
Output:
The main role of the GOTO/FROM blocks is to transport signals from a block to another block
without connecting them physically. The GOTO block transports its input data to its corresponding
FROM block. A simple GOTO block can send data to multiple FROM, although a FROM can receive
data from only one GOTO.
The GOTO and FROM blocks are connected by the tag parameter.
The "Tag Visibility" parameter indicates if the location of the FROM block is limited:
- Local: means that the corresponding FROM of that GOTO must be in the same subsystem.
programming-with-scilab.blogspot.com 4/14
7/26/2019 Programming with Scilab
- Scoped: means that the corresponding FROM of that GOTO must be in the same subsystem or in
any subsystem below the GotoTagVisibility block in the model hierarchy.
- Global: means that the corresponding FROM of that GOTO can be anywhere in the model.
This block can support all the data types.
Blocks in green are GOTO blocks. Blocks in orange are FROM blocks. These blocks are available in
Xcos > palettes > Signal routing palette
programming-with-scilab.blogspot.com 5/14
7/26/2019 Programming with Scilab
CSV is a common, relatively simple file format that is widely supported by consumer, business, and
scientific applications.This works because so many programs support some variation of CSV at least
as an alternative import/export format.CSV formats are best used to represent sets or sequences of
records in which each record has an identical list of fields.
The CSV file format is very simple and supported by almost all spreadsheets and database
management systems. Many programming languages have libraries available that support CSV files.
Scilab can import/export CSV files. The built-in function "read_csv" and "write_csv" used for import
and export data from/to CSV files. But the execution time is more. The solution for this problem is
CSV utilities toolbox
programming-with-scilab.blogspot.com 6/14
7/26/2019 Programming with Scilab
Result:
programming-with-scilab.blogspot.com 7/14
7/26/2019 Programming with Scilab
Looking at the results, CSV Utilities toolbox provides the flexibility of import/export larger data
from/to CSV files.
Scilab Program
//Generating a set of random numbers distributed within the square in the XY plane
//bounded by (-1,1),(1,1),(1,-1),(-1,-1)
x=2*rand(2,20)-1;
x1=x(1,:);//Array x1 contains the x coordinates of the ten points
y1=x(2,:);//Array y1 contains the y coordinates of the ten points
plot(x1,y1,'*');
x2=linspace(-1,1,100);
for i=1:100
programming-with-scilab.blogspot.com 8/14
7/26/2019 Programming with Scilab
y2(i)=2*x2(i);
end
plot(x2,y2,'r');
//initial weights
w=[0;0];
//Classify the points as the ones to the right or left of the line
for i=1:10
l(i)=F(2)*y1(i)+F(1)*x1(i);
class_F(i)=sign(l(i));
end
while ~isempty(ind)
for i=1:10
w(1) = w(1) + class_F(ind(1)).*x1(ind(1));
w(2) = w(2) + class_F(ind(1)).*y1(ind(1));
end
//temp = sign(w'*x);
for i=1:10
temp(i)=sign(w(2)*y1(i)+w(1)*x1(i));
end
ind = find(temp ~= class_F);
iter = iter + 1;
end
for i=1:100
y3(i)=-(w(2)/w(1))*x2(i);
end
plot(x2,y3,'g');
programming-with-scilab.blogspot.com 9/14
7/26/2019 Programming with Scilab
The Newton-Raphson method consists in obtaining improved values of the approximate root through
the recurrent application of the equation. The iterative procedure can be generalized in the form
below
After each iteration the program should check to see if the convergence condition
is satisfied.
Algorithm Description
The iterative algorithms must include the option of stopping the program if the number of iterations
are too large. The number of iterations will depend of the particular problem solved. However, a
computer program to implement the Newton-Raphson method could take more than 1000 iterations
to converge if the problem is either ill-posed or if it has a logical error. In such cases, the initial values
need to be modified to ensure convergence.
programming-with-scilab.blogspot.com 10/14
7/26/2019 Programming with Scilab
Example Problem
We see that the function graph crosses the x-axis somewhere between -0.8 and -0.4, between 0.8
and 1.2, and between 1.6 and 2.0.
The Scilab Program to implement the algorithm to find the roots of a polynomial using
Newton Raphson Method
programming-with-scilab.blogspot.com 11/14
7/26/2019 Programming with Scilab
The results of the newton_raphson algorithm in SCILAB console are shown below:
More about
Newton
Raphson
Method at http://en.wikipedia.org/wiki/Newton%27s_method
programming-with-scilab.blogspot.com 12/14
7/26/2019 Programming with Scilab
Fermat's Spiral
The January issue of the Economist magazine carried a beautiful article that talks about at least
three interesting themes
1) How to make solar farms more efficient by design ?
2) How we continue to learn from nature ?
3) What is the math in the floral arrangement in sunflower ?
"Is there more to the sunflower than the Fibonacci series ?"
in polar coordinates (the more general Fermat's spiral follows r 2 = a 2θ.) It is a type of Archimedean
spiral.
Code snippet:
a=2;
theta=0:(2*%pi)/100:(10*%pi);
r_pos=a*(theta.^(1/2));
r_neg=-a*(theta.^(1/2));
polarplot(theta,r_pos,[1,2],"170");
polarplot(theta,r_neg,[1,2],"170");
Output Plot:
programming-with-scilab.blogspot.com 13/14
7/26/2019 Programming with Scilab
Home
programming-with-scilab.blogspot.com 14/14