Getting Started with the Math Software Scilab
Actually, I'd wanted to learn a piece of math software for quite a while. Back in the day I was really into Mathematica and fiddled around with it for a bit, but since I didn't have much need for it in high school, I never kept it up. More importantly, all these programs cost money. After starting university, I heard my seniors talk about mathematical modeling, and it turned out they were mostly using Mathematica or MATLAB — but both of these are paid software, and I didn't really want to use cracked versions. Since I was already using Ubuntu, I figured I should make good use of it. I'd heard that there's software with commands very similar to MATLAB's, namely Scilab, and also Octave — the difference being that these are both open-source and free.
For the purpose of getting familiar with coding and mathematical software programming, I chose to learn Scilab. Although people online say Octave resembles MATLAB more closely, I felt that Scilab is more widely used than Octave, so I went with it. As the saying goes, "master one principle and all others become clear" — better to focus and get good at one first.
Below is my first Scilab program, which uses Wilson's method to do a primality test. The main purpose of this code was to practice conditional statements and loop statements, along with a few input/output tricks. The program itself is rather ugly.
//我的第一个scilab程序
//完成于2012.09.27
label1=['p:';]; //定义标签
B=x_mdialog(['本程序使用威尔逊方法判断进行素数测试。';'请输入要判断的数'],label1,['127';]); //输入框
p=evstr(B(1)); //提取输入框里边的数字进行赋值
i=1;
j=1;
q=p-1;
while i<q
j=j*i;
j=modulo(j,p);//这个是模函数。
i=i+1;
end
if j==1
messagebox(['这是一个素数';],['测试结果']); //输出,其中后边的“测试结果”是输入框的标题
else
messagebox(['这是一个合数';],['测试结果']);
end
This is my second program, mainly meant to reinforce memory and practice the algorithm. The program uses Newton's method to solve the cubic equation $x^3+ax^2+bx+c=0$, without doing any error checking, since I felt that such details weren't my main priority at this stage.
//我的第二个scilab程序
//完成于2012.09.28
label1=['a:';'b:';'c:';'初始值';'精确度';]; //定义标签
B=x_mdialog(['本程序使用牛顿法求解x^3+ax^2+bx+c=0';'请输入系数、初始值、精确度'],label1,['1';'1';'1';'1';'0.001';]); //输入框
a=evstr(B(1)); //提取输入框里边的数字进行赋值
b=evstr(B(2));
c=evstr(B(3));
d=evstr(B(4));
q=evstr(B(5));
x=d;
e=x^3+a*x^2+b*x+c;
while abs(e)>q
x=x-e/(3*x^2+2*a*x^2+b);
e=x^3+a*x^2+b*x+c;
end
x//输出答案
Below is my third Scilab program, written to solve a problem from the "Mathematics R&D Forum." The problem is as follows:
http://bbs.emath.ac.cn/viewthread.php?tid=3731
The task is essentially to output a sequence of primes where the last n-1 digits of the n-th term equal the (n-1)-th term of the sequence. For example: 7, 17, 317, 6317, ...
Since this requires primality testing, and I couldn't find a built-in prime-checking function in Scilab, I first defined my own function, prime(), which uses the Sieve of Eratosthenes to determine primality.
//我的第三个scilab程序
//输出素数数列,其中第n个的后n-1位是数列中的第n-1个数
//完成于2012.09.28
function p=prime(x)
p=1
for i=3:2:x^0.5;
if modulo(x,i)==0 then
p=0;
break
end
end
endfunction
//上面是自定义新函数来进行素数判断。素数则输出1,合数则输出0
//被判断数要大于9
a=7
j=0;
while j<8
if prime(a)==1
a
j=j+1;
a=a+10^j;
else
a=a+10^j;
end
end
The above are all rough works from when I first started learning Scilab, kept mainly as a memento — experts, please feel free to skip over them. Once I'm more familiar with Scilab's various functions and structures, I'll try to apply programming as much as possible to problems in physics and other sciences. Of course, I'd welcome any guidance from those more experienced than me.
Translated automatically with claude-sonnet-5; all equations are reproduced verbatim from the source. Copyright remains with the original author.