Actually, I have wanted to learn how to use a mathematical software for a long time. I was very interested in Mathematica before and played around with it for a while, but since there wasn’t much need for it in high school, I didn’t stick with it. More importantly, these softwares are not free. After entering university and hearing seniors talk about mathematical modeling, I found that they basically use Mathematica or Matlab as well. However, both of these softwares require payment, and I don’t really want to use cracked versions. Since I am already using Ubuntu, I should make good use of it. It is said that Scilab and Octave have commands very similar to Matlab, but the difference is that these are open-source and free.
For the purpose of becoming familiar with code operations and mathematical software programming, I chose to learn Scilab. Although it is said online that Octave is more similar to Matlab, I feel that Scilab is more widely used than Octave, so I am using it. As the saying goes, “Once you understand one principle, you understand them all”; I will focus on learning one well first.
Below is the first Scilab program I wrote, which uses Wilson’s method for primality testing. The main purpose of this code is just to practice conditional and loop statements, as well as some input and output techniques. The program itself is quite crude.
// My first Scilab program
// Completed on 2012.09.27
label1=['p:';]; // Define label
B=x_mdialog(['This program uses Wilson''s method for primality testing.';'Please enter the number to be tested'],label1,['127';]); // Input dialog
p=evstr(B(1)); // Extract the number from the input box and assign it
i=1;
j=1;
q=p-1;
while i<q
j=j*i;
j=modulo(j,p); // This is the modulo function.
i=i+1;
end
if j==1
messagebox(['This is a prime number';],['Test Result']); // Output, where "Test Result" is the title of the message box
else
messagebox(['This is a composite number';],['Test Result']);
end
This is my second program, mainly to reinforce my memory and practice algorithms. The program uses Newton’s method to solve the cubic equation x^3+ax^2+bx+c=0, without any error checking. I feel that focusing on details is not my primary task at the moment.
// My second Scilab program
// Completed on 2012.09.28
label1=['a:';'b:';'c:';'Initial Value';'Accuracy';]; // Define labels
B=x_mdialog(['This program uses Newton''s method to solve x^3+ax^2+bx+c=0';'Please enter coefficients, initial value, and accuracy'],label1,['1';'1';'1';'1';'0.001';]); // Input dialog
a=evstr(B(1)); // Extract numbers from the input box and assign them
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 // Output the answer
Below is my third Scilab program. It was written to
solve a problem on the “Math Research Forum”. The problem is as
follows:
http://bbs.emath.ac.cn/viewthread.php?tid=3731
The main goal is to output a sequence of primes where the last n-1 digits of the n-th number are the (n-1)-th number. For example, 7, 17, 317,
6317, …
Since primality testing is required and I couldn’t find a primality
testing function in Scilab, I first defined a new function
prime(), using the Sieve of Eratosthenes to determine if a
number is prime.
// My third Scilab program
// Output a sequence of primes where the last n-1 digits of the n-th number are the (n-1)-th number
// Completed on 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
// The above is a custom function for primality testing. Outputs 1 for prime, 0 for composite.
// The number to be tested should be greater than 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 my humble works from when I just started learning Scilab, intended for commemorative purposes. Experts and advanced users, please ignore them. Once I become more familiar with Scilab’s various functions and structures, I will try to apply programming to scientific problems such as physics as much as possible. Of course, I hope for guidance from the experts.
When reprinting, please include the address of this article: https://kexue.fm/archives/1720
For more detailed reprinting matters, please refer to: Scientific Space FAQ