0% found this document useful (0 votes)
97 views

Programming With SCILAB

The document describes using Scilab to implement a perceptron algorithm to classify data points. It generates random points in a 2D plane, defines a separating line, initializes weights, classifies points, and runs an iterative learning algorithm to adjust the weights until the classifications match the defined line. Scilab scripts are provided to generate sample data, classify points, learn weights through iterations, and plot the results.

Uploaded by

Sabha Nayagham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Programming With SCILAB

The document describes using Scilab to implement a perceptron algorithm to classify data points. It generates random points in a 2D plane, defines a separating line, initializes weights, classifies points, and runs an iterative learning algorithm to adjust the weights until the classifications match the defined line. Scilab scripts are provided to generate sample data, classify points, learn weights through iterations, and plot the results.

Uploaded by

Sabha Nayagham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

7/26/2019 Programming with Scilab

More stsabha@gmail.com Dashboard Sign Out

Programming with Scilab


TechPassion Blog

View my complete profile

Thursday, April 19, 2012 Blog Archive

▼ 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

Posted by TechPassion Blog at 6:08 AM No comments:

Labels: Example 1

Recursive function implementation in Scilab


Recursion is a method where the solution to a problem depends on solutions to smaller instances of
the same problem. The approach can be applied to many types of problems.

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:

Thus, like other programming languages, One can write


recursive program in Scilab to reduce the complexity of
program.

Posted by TechPassion Blog at 4:23 AM No comments:

Tips to Write XCOS program: Use of GOTO/FROM blocks


While making a Xcos program, We may get messy Xcos diagram because of the blocks location and
the links which connects those blocks.

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.

This block can support all the data types.

Below a simple Xcos daigram which describes the problem.

Below the use of GOTO and FROM blocks

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

Posted by TechPassion Blog at 3:16 AM 1 comment:

Tuesday, April 17, 2012

CSV File Handling


A comma-separated values (CSV) file stores tabular data (numbers and text) in plain-text form.
Plain text means that the file is a sequence of characters, with no data that has to be interpreted
instead, as binary numbers. A CSV file consists of any number of records, separated by line breaks
of some kind; each record consists of fields, separated by some other character or string, most
commonly a literal TAB or comma.

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

Scilab instruction to import/export data from/to CSV files:

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.

Posted by TechPassion Blog at 10:18 PM 2 comments:

Saturday, April 14, 2012

Perceptron using Scilab


Wikipedia on Perceptron

Example Problem: Classification of a set of points in the XY Plane

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,'*');

//Plotting an arbitrary line y-2x=0 f(x): y-2x=0


//Save the coefficients of the x and y terms in an array F
F=[1;-2];

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

//Calculate the sign of the hypothesis : g(x):w1*x1+w2*y1


for i=1:10
g(i)=sign(w(2)*y1(i)+w(1)*x1(i));
end

//Learning Algorithm Iteration


ind = find(class_F ~= g); // Find the indices where temp is not equal to f
iter = 1;

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');

The output plot showing the desired function, hypothesis function

for a set of twenty points chosen randomly in the XY plane

within the square bounded by (1,1),(1,-1),(-1,1),(-1,-1)

programming-with-scilab.blogspot.com 9/14
7/26/2019 Programming with Scilab

Posted by TechPassion Blog at 1:10 PM No comments:

Monday, April 9, 2012

Newton-Raphson Method to find the roots of a polynomial


Reference:

Scilab Reference Manual by Professor Gilberto E. Urroz, Ph.D., P.E.

Consider the following single nonlinear equation

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

Polynomial : f(x) = x^3-2*x^2+1

To visualize the solution, it helps to plot the function using Scilab.


Use the following scilab commands to plot the function

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

Posted by TechPassion Blog at 11:33 PM 2 comments:

programming-with-scilab.blogspot.com 12/14
7/26/2019 Programming with Scilab

Monday, January 23, 2012

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 ?

# Theme 1 : How to make solar farms more efficient by design ?


Engineers, Scientists and Researchers are constantly striving to extract more efficiency from
solar farms. It is well established that the concentrating solar power (CSP) technology is a leading
technology in the solar power industry. CSP technology involves the application of several mirrors
arranged in a pattern to concentrate sun’s energy onto a central boiler that converts water to steam
to drive a turbine.
The question is : "What is the best design to arrange these mirrors ?"

# Theme 2 : How we continue to learn from nature ?


It turns out that the floral arrangement in sunflower provides a clue to optimize the arrangement of
the mirrors to get the most efficiency. When we think of sunflower, we think of Fibonacci series.

"Is there more to the sunflower than the Fibonacci series ?"

More on Fibonacci series at http://www.popmath.org.uk/rpamaths/rpampages/sunflower.html

# Theme 3 : What is the math in the floral arrangement in sunflower?


Fermat’s spiral is what we are referring to.

So, whats the math in the Fermat's Spiral

Fermat's spiral (also known as a parabolic spiral) follows the equation

in polar coordinates (the more general Fermat's spiral follows r 2 = a 2θ.) It is a type of Archimedean
spiral.

Scilab is an open source, cross-platform numerical computational package and a high-level,


numerically oriented programming language. It can be used for creation of Fermat's 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

Posted by TechPassion Blog at 9:55 PM No comments:

Home

Subscribe to: Posts (Atom)

Ethereal theme. Powered by Blogger.

programming-with-scilab.blogspot.com 14/14

You might also like