RE: C++ to Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg30843] RE: [mg30812] C++ to Mathematica
- From: "Barthelet, Luc" <lucb at ea.com>
- Date: Thu, 20 Sep 2001 03:51:40 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
1) this code does not compile in C++, it is full of typos.
2) here is what I believe to be the translation in Mathematica:
minErr = MAXERR;
bestM = STARTM;
numConvergence = 0;
For[c0 = avgc0 -.5, c0 <= avgc0 +.5, c0 += 0.01,
For[ M = -1.0, M <= 1.0, M +=0.01,
sum = 0.0;
num = 0;
c02 = c0 * c0;
For[j=0,j<10,j++,
If[(avgc0>0),p=avgp[j]; c=avgc[j]; sum+=
fabs(sqrt(c02 - M * p *p)-c); num++;]
];
If[(num<=0), Continue];
err = sum/num;
If[(err <minErr), bestc0=c0; bestM = MMinErr =
err;numConvergence++;];
];
];
If[(numConvergence==1), STARTM, bestM]
Hope this motivates you to learn mathematica (or C++).
Cheers,
Luc
-----Original Message-----
From: Moranresearch at aol.com [mailto:Moranresearch at aol.com]
To: mathgroup at smc.vnet.net
Subject: [mg30843] [mg30812] C++ to Mathematica
Could someone please help convert this C++ code to Mathematica 4.1. I am
unfamiliar with the loop structures and commands of C++. Even if there are
easier ways to do this in M4.1 ( which, by the way, I would like to know
also) I want to reproduce this as closely as possible so I can verify the
answer with the older C++ program.Thank you.
minErr = MAXERR; bestM = STARTM, numConvergence = 0
for(c0 = avgc0 -.5; c0 <= avgc0 +.5; c0 += 0.01){for M = -1.0; M <= 1.0; M
+=0.01)
{sum = 0.0; num = 0; c02 = c0 * c0;
for(j=0;j<10;j++){
if{avgc0>0){
p=avgp[j]
c=avgc[j]
sum+= fabs(sqrt(c02 - M * p *p)-c);num++;}}
if(num<=0) continue;
err = sum/((double)num;
if(err <minErr){
bestc0=c0;
bestM=M
MinErr = err;
numConvergence++;}}}
if(numConvergence==1) return STARTM;
return bestM