数学建模社区-数学中国
标题:
mathematica c语言混合编程的问题 大神求救
[打印本页]
作者:
遥望科西嘉
时间:
2014-1-16 17:36
标题:
mathematica c语言混合编程的问题 大神求救
目的是算出cos(x)5次方的程序,整个程序为,结果却不对,调试了很长时间也没有搞定,向大神求助了
8 d+ S2 b4 o5 ^
#include "stdafx.h"
/ s0 \# n; A. N9 d( i0 j
5 v4 _/ ]7 B! M* H
#include <stdio.h>
5 J; O' I' a. H# r
#include <stdlib.h>
# R! m% v9 h- T4 V+ D2 S
#include <string.h>
$ _# T! v+ }* i( p2 _
/ V+ i/ v, o. q# L5 e
#include "mathlink.h"
' B% {7 h7 X1 s [ h
+ |1 u5 i3 ]- w
double f(MLINK lp,double x);
% Z( O) t8 {; A# F
( F5 C7 V+ h6 n
int main()
9 ]( K+ w, H' v1 ?
{
2 W! F6 T2 y$ r4 K; ~7 L( c
int argc=4;
. U# A3 `( R7 o D* w* r
char*argv[5] ={"-linkname", "F:\\Program Files\\Wolfram Research\\Mathematica\\8.0", "-linkmode", "launch", "NULL"};//指定连接方式
$ f, ^/ d& ]! q7 [" Q1 D' d9 ]7 y
MLEnvironment env;//mathlink的环境变量
! U6 G; \" K! l
MLINK lp;
8 |6 s1 s# Q$ [8 Q% G
env=MLInitialize(NULL);//初始化
p- E! p7 ?: g/ [$ e" W! F+ ~, j* Q
if(env==NULL)
( {! K, |3 M0 I' i
{
]% j1 v0 V1 O2 p
printf("initialization error!\n");
R( F; T% K) A7 M, [
return 1;
/ J$ B# h: A: @& U7 f N, |
}
$ {7 E4 K0 }6 h5 z h
lp=MLOpen(argc, argv);
1 c( f: s. d+ N c7 a5 _! ]* O
if(lp==NULL)
" n6 F' Q; X4 ]& S3 n
{
; x3 U5 F. H+ ^% E0 |
printf("open error!\n");
- q* p Y1 H z* h$ U! A0 x; e
return 1;
( t/ [9 S0 h( z9 m# ?
}
# a" q5 |* d1 f- t8 G& ^# _6 N' f
for(int i=1;i<=10;i++)
! l" q8 Z& y9 I# }9 h+ Y
printf("cos(%d)= %lf\n",i,f(lp,i));//计算f(1)至f(10)
# }2 x0 r: S' Y- f; L
2 D9 M# x" \; ?+ S& }$ ]
MLClose(lp);
! W* L: A: k# }9 V R( V$ b* L
MLDeinitialize(env);
: l, |* ^0 a5 \, ?9 Z2 E
8 i! [; }" V# d3 ?1 {$ b, W" P
//printf("Hello World!\n");
* ~& t4 F8 E7 A8 P& {0 b
return 0;
( |4 t8 \: z2 M. K4 N) [' G$ o
}
& Y) Q4 G+ A ]; s: x
& n. H( |2 e1 K2 p5 L
2 H6 k& y( d, q
double f(MLINK lp, double x)
, D8 B7 a* w0 ~' v
{
- Y% `* i* Z) s2 D
double res;
8 ~3 H, l* x5 I/ }2 `
int pkt;
6 O% k8 ^, \# i: B
static int done=0;
g; n) d$ t& y$ k) r. @+ g
if(!done)
! g1 d P; x: t+ ^
{
% R" ?+ J. z& A1 V, K3 P
//char* expression="cos(x)^5";
6 [1 @6 n5 r0 O7 ?! q4 {2 Q. l$ S
MLPutFunction(lp,"EvaluatrPacket",1);
v; _) _0 T) ` H0 _! B l
MLPutFunction(lp,"ToExpression",1);
5 \6 `2 a$ N0 `8 ]
MLPutString(lp,"cos(x)^5");
4 a# b- o6 d3 c* g5 v
MLEndPacket(lp);
. Z5 ~& n, G% v* e# Z
while((pkt=MLNextPacket(lp),pkt)&&pkt!=RETURNPKT);
* V5 @$ H0 `" J
MLNewPacket(lp);
: x2 k4 x/ X$ g7 T" m% k
done=1;
4 T& Z+ k6 A4 X7 B
}
' k, [. c$ B+ o8 F
MLPutFunction(lp,"EvaluatePacket",1);
: B( O: A# Z: ? `3 k+ X4 V
MLPutFunction(lp,"N",1);
# I; k* N! S) v! I$ Q; p1 M( L
MLPutFunction(lp,"F",1);
1 l7 S% m% T* U( ]( \5 w4 h" e3 q
MLPutReal(lp,x);
1 `% u; \1 Z( w: T$ |. d$ w
MLEndPacket(lp);
. N/ N E5 E. Q1 @/ X
while((pkt=MLNextPacket(lp))&&pkt!=RETURNPKT);
/ f& Y* V* @1 g7 {8 S9 C' h7 Q: n
MLNewPacket(lp);
. Y* O+ n: p/ P8 j; Y2 G0 z% G
MLGetReal(lp, &res);
3 f# J- M0 R/ j: y9 ]$ z8 E
return res;
1 O# ^# z: Y0 R- ?; G& m5 Y- j4 G
}
; x4 {- V+ n1 F/ _! |+ I% z
4 U/ p9 s! v' {% q9 L) L' ]
作者:
遥望科西嘉
时间:
2014-1-16 17:44
我觉得是不是因为在mathematica上面还要写个接口,提供c数据交换的接口
作者:
遥望科西嘉
时间:
2014-1-20 18:49
这么大个论坛没人了么?
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5