数学建模社区-数学中国

标题: mathematica c语言混合编程的问题 大神求救 [打印本页]

作者: 遥望科西嘉    时间: 2014-1-16 17:36
标题: mathematica c语言混合编程的问题 大神求救
目的是算出cos(x)5次方的程序,整个程序为,结果却不对,调试了很长时间也没有搞定,向大神求助了
: S$ f" |8 T. q2 C#include "stdafx.h"( A6 _! @4 m1 t- s, w! I2 P) U

; U4 d6 T( D9 p5 m' Y" C4 j#include <stdio.h>
# n8 X/ ?$ B" \1 D' ~#include <stdlib.h>
8 e2 d* W. n) o. y9 ?3 ^0 e' E! `* d#include <string.h>* Y5 _( G1 Q  T) R

6 F' }7 R, l! N$ d; s. e#include "mathlink.h"
" L! |# w2 _: n. L0 W- k
1 j9 W* K0 }) h6 U' {- `. Ddouble f(MLINK lp,double x);$ B0 v; x6 h' R% G; B

7 b8 I6 T) o3 V& \2 E4 t& }int main()
# \  A: J/ \% Q! M/ L& R{
' {/ V% k3 c  V' n$ z+ B        int argc=4;
6 d$ @+ H* a+ z0 m/ w) {9 L        char*argv[5] ={"-linkname", "F:\\Program Files\\Wolfram Research\\Mathematica\\8.0", "-linkmode", "launch", "NULL"};//指定连接方式
0 T6 Y; H4 l6 X        MLEnvironment env;//mathlink的环境变量
# t! S* }0 S- |4 i4 ?        MLINK lp;, ?1 g- C+ n& U; G- {( [" W
        env=MLInitialize(NULL);//初始化
" b$ ]/ \5 i; C" U" r        if(env==NULL)& h/ t+ K0 ~% J
        {9 A# _( i& B8 f6 z4 G, u  w) o7 I
                printf("initialization error!\n");
: C6 k# H9 m% V! L8 U* U+ b: f! t2 ]                return 1;
2 s; `2 E7 `* A( K  `+ S        }2 @- ~9 C$ ^0 P! S  `' g; c
        lp=MLOpen(argc, argv);8 I4 H9 _# r  }, M  W1 l
        if(lp==NULL)- I2 @4 L. @; g' J8 W
        {
. U$ g. O/ ?; }" w! R- b1 H8 [; ?                printf("open error!\n");
2 Q, z6 r+ }1 W9 q( O                return 1;
; C+ ^6 {" H. k1 |% m0 w, U% C        }  a; o" B  X1 E' j
        for(int i=1;i<=10;i++)
9 x. ~) L# c7 `2 T! d! c: e7 v* I                printf("cos(%d)=  %lf\n",i,f(lp,i));//计算f(1)至f(10): {& b" c% G. G( B0 ]
$ a. Y  T/ F' S
        MLClose(lp);
. O  z4 V1 ~( T1 P        MLDeinitialize(env);
6 n' p7 ?% d4 O$ ^! r  U' i; _
; Z" t% @5 u9 W0 r7 Q        //printf("Hello World!\n");, ^5 Q, b5 U. o
        return 0;
, F2 o1 I' d9 F- Z" o}
2 _3 G6 p, s( e" k# T" i# a
1 p2 k3 F1 z9 M2 D/ u+ s7 L8 s' {  M
double f(MLINK lp, double x)$ U9 W# l$ m6 I  E4 r9 f
{
/ H1 U) U8 u# }' U6 w6 k; k/ D8 k# f        double res;
. J5 m. i  }0 y, O& F! L; O5 v        int pkt;
0 b- \% \/ H- u2 `  i        static int done=0;  L; @: R6 l4 P0 M& O' V8 c
        if(!done)
; Y- q# U6 {, x1 b* J% r+ ?        {
& d0 Z0 `8 k  ^( o                //char* expression="cos(x)^5";
, Y, {- r# {1 u                MLPutFunction(lp,"EvaluatrPacket",1);& _! |- B/ G5 `; X
                MLPutFunction(lp,"ToExpression",1);
/ \: C6 V- g. q! i                MLPutString(lp,"cos(x)^5");1 `: ?. q, s" o, L
             MLEndPacket(lp);
0 r$ J9 }, j5 z! P6 w6 L                while((pkt=MLNextPacket(lp),pkt)&&pkt!=RETURNPKT);
8 |/ t$ M2 z8 f5 }- q. }- G8 H: w                MLNewPacket(lp);
& J8 Q7 z8 l7 T- b% s" [9 S; X                done=1;: T: H# t: C# R% H! J! {
        }6 B. }7 z: f0 Z1 h. V
        MLPutFunction(lp,"EvaluatePacket",1);
; r& t0 }! n+ ], W        MLPutFunction(lp,"N",1);0 G% R/ x2 D3 d" Y7 g; B
        MLPutFunction(lp,"F",1);
3 H# e9 W1 o. D/ `$ D  `" ^        MLPutReal(lp,x);
! e0 X. t" l/ a2 v& ^. T4 n        MLEndPacket(lp);2 S! _# ?/ O- P& k2 l% i8 ?/ r7 u
        while((pkt=MLNextPacket(lp))&&pkt!=RETURNPKT);
! y6 X6 ^/ i9 j9 h4 V+ j3 D        MLNewPacket(lp);7 v, q# G  g7 q5 `: ~
        MLGetReal(lp, &res);
2 n0 T2 J& \6 G9 B1 q        return res;
' y/ {2 r) _# k" d2 c}& `7 E; P0 s& g
2 A4 e* Q" x6 k0 n) h/ d

作者: 遥望科西嘉    时间: 2014-1-16 17:44
我觉得是不是因为在mathematica上面还要写个接口,提供c数据交换的接口
作者: 遥望科西嘉    时间: 2014-1-20 18:49
这么大个论坛没人了么?




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5