how to do simple loop in maxima and save the result in a file?
Subject: how to do simple loop in maxima and save the result in a file?
From: Edwin Woollett
Date: Wed, 20 Apr 2011 11:53:55 -0700
On April 20, 2011, RAZIF RAZALI wrote:
==============================
.................
delt=0.001;
fout = fopen( "test1.dat", "w");
for (x=0.4; x<1; x=x+delt){
p=(1-x)/(1+x);
printf ("%f %f\n",p,x);
fprintf(fout, "%f %f\n",p,x);
................
how can I do similar loop program in Maxima and save the result in a new
file like 'test1.dat'?
===============================
Experiment with write_data and printfile.
As a simple first step,
(%i3) (dataL:[],
for x thru 5 do (
px : subst (x,z,(1-z)/(1+z)),
dataL : cons ([x,px],dataL)),
dataL : reverse (dataL),
write_data (dataL,"mydata1.dat"))$
(%i4) printfile ("mydata1.dat")$
1 0
2 -1/3
3 -1/2
4 -3/5
5 -2/3
note that write_data creates the file if the file
does not already exist and overwrites previous
file contents if the file already exists.
I use windows, and my default app for *.dat is Notepad,
which expects CR,LF line endings. Maxima writes
with LF line endings, so Notepad will show the
single line
1 02 -1/33 -1/24 -3/55 -2/3
To see the expected double column data file
you should use Notepad2 (free).
To deal with your case, you should include
while, as in this simple example:
(%i8) (dataL:[],
for x: 0.1 step 0.1 while x < 1 do (
px : subst (x,z,(1-z)/(1+z)),
dataL : cons ([x,px],dataL)),
dataL : reverse (dataL),
write_data (dataL,"mydata2.dat"))$
(%i9) printfile("mydata2.dat")$
0.1 0.81818181818182
0.2 0.66666666666667
0.3 0.53846153846154
0.4 0.42857142857143
0.5 0.33333333333333
0.6 0.25
0.7 0.17647058823529
0.8 0.11111111111111
0.9 0.052631578947368
1.0 5.5511151231257827E-17
In Chapter 2 of Maxima by Example, I show how
to import file data into a list inside Maxima, and
use it for fits and plots.
In light of your query, I should probably add a
section related to writing data to a file.
Best Wishes,
Ted Woollett
http://www.csulb.edu/~woollett