Java runtime error because of SYMS variables..

Hello,
I have a functions follow as
function[out1,out2,..]=myProject(in1,in2,in3...)
....
syms Hs Ht; // HERE LİNE 21
CT=C0*(C1+C2*((1/Hs)+(1/Ht)+C3))+C7*(Hs^4.1+((Hs^5.1)/Ht)+C3*(Hs^5.1))+C8*(((Ht^3.5)/Hs)+(Ht^2.5)+C3*(Ht^3.5));
F1=diff(CT,Hs);
F2=diff(CT,Ht);
....
end
after that I used this function in my java program.
MyMatlabClass myObj = new MyMatlabClass();
outObj=myObj.MyProject(27,in1,in2,in3,...);
everything as great as here.But when program run i took a error as follows..
Error in MyProject (line:21)
:S Actually i do not understand Why program has failed? I'm waiting your solutions..
Thanks..

2 Comments

Jan
Jan on 12 Feb 2013
Edited: Jan on 12 Feb 2013
Is this the complete error message? Usually error message do not contain only the line number, but some descritpions also.
Which is the line 21?
Firstly Thanks for interested.Yes This error message when program run i took from Netbeans. by the way I specified up
syms Hs Ht; // HERE LİNE 21

Sign in to comment.

 Accepted Answer

From java... Is this using the Java Builder project? If so then the problem is that the symbolic toolbox cannot be compiled, and so cannot be used from the NET Builder or JAVA Builder products.

12 Comments

hello,
yes,Firstly I took .jar file through Matlab Java builder then I added this jar file to project jar library..
So What can I use instead of SYMS variables?? :S
Nothing in the Symbolic Toolbox can be used in Java Builder.
With the bit of code you supplied, it looks to me as if possibly the form and exponents are all fixed, and only the constants vary. If so then you should be able to take the general form of the expression with symbolic variables in place of the constants, do the differentiation symbolically, grab the result and code that form in your actual program so that no differentiation is needed at run-time. You might want to use matlabFunction()'s option to output code to a file to generate the expression that you include in the compiled program.
Thanks but sorry really Can you explain simply sentences.I did'nt understand not more,bu the way this is my all matlab code..
function [u,Nt,A,L,DPt,Ds,Lb,Nb,v,Lbc,Nbc,DPs,Cop,Che,As,Reo,Pb,H,xc,ndik,Fn,Fb,n0,jb,Dpidealdik,Aw,Ww,Wz,Dctl,nvw,Dpw,Dpe,Dpgovdebell] = myProject(Cp1,Cp2,n1,n2,p1,p2,k1,k2,P1,P2,m1,m2,Di,D0,R,Pt,Cn,F,t,n,ng,nv,Tb1,Tb2,Tg1,Tg2,i)
DT=abs((DT1-DT2)/log(DT1/DT2));
Q=abs(m1*Cp1*(Tb2-Tb1)*1000);
C0=i*((1+i)^n)/(((1+i)^n)-1);
C1=450;
C1a=202;
C1t=0.092*(p1/Di)*((p1*Di)/n1)^(-0.2);
C2t=0.023*(k1/Di)*(P1^(1/3))*((p1*Di)/n1)^0.8;
Ct=C1t*(Di^2)/(4*Vt*D0*(C2t^3.5));
De=(3.464*(Pt^2)-(pi*(D0^2)))/(pi*D0);
C1s=(1.79*((n2/De)^0.19)*(p2^0.81))/(2*De);
C2s=(4*Cn*Pt*(Pt-D0))/((pi^2)*D0*Vs);
C3s=0.36*(k2/De)*(P2^(1/3))*((p2*De)/n2)^0.55;
C3=0.00070;
Cs=(C1s*C2s)/(C3s^5.1);
C2=(C1a*Q)/DT;
C4=(t*F)/(1000*nv);
C5=(Cs*Q)/DT;
C6=Ct*(Q/(DT));
C7=C4*C5*Vs;
C8=C4*C6*Vt;
syms Hs Ht
CT=C0*(C1+C2*((1/Hs)+(1/Ht)+C3))+C7*(Hs^4.1+((Hs^5.1)/Ht)+C3*(Hs^5.1))+C8*(((Ht^3.5)/Hs)+(Ht^2.5)+C3*(Ht^3.5));
F1=diff(CT,Hs);
F2=diff(CT,Ht);
xo=[1;1] ;
fname=[F1;F2];
f_ilk=jacobian(fname);
epsilon=1.e-10;
maxiter = 30;
iter = 1;
f=inline(fname);
jf=inline(f_ilk);
error=norm(f(xo(1),xo(2)),2);
% fprintf('error=%12.8f\n', error);
while error >= epsilon
fxo=f(xo(1),xo(2));
fpxo=jf(xo(1),xo(2));
x1=xo-inv(fpxo)*fxo;
fx1=f(x1(1),x1(2));
error =norm((fx1),2);%abs(x1-xo);
if iter > maxiter
fprintf('İterasyon failed \n');
return;
end
xo=x1;
iter=iter+1;
end
Ht=x1(1);
Hs=x1(2);
p_us=1/0.8;
u=((Ht/C2t)^(p_us));
Nt=Vt*ng/((pi/4)*(Di^2)*u);
K=1/(1/Hs+1/Ht+R);
A=Q/(K*DT);
L=A/(pi*Nt*D0);
DPt=Ct*A*(Ht^(3.5));
v=(Hs/C3s)^(1.82);
Ds=(((Nt*4*(Pt^2)*Cn)/pi))^(0.5);
Lb=(Pt*Vs)/((Ds*v)*(Pt-D0));
Nb=(L/Lb)-1;
Lbc=(Vt*Pt)/((Pt-D0)*(v*Ds));
Nbc=(L/Lbc)-1;
DPs=Cs*A*(Hs^(5.1));
Cop=C4*((DPs*Vs)+(DPt*Vt));
Che=C0*(C1+C2*((1/Hs)+(1/Ht)+C3));
As=((Pt-D0)*Lbc*Ds)/Pt;
Reo=((v*D0)*p2)/n2;
Pb=Pt*0.8660254038;
H=Ds*(25/100);
xc=Ds-H;
ndik=xc/Pb;
Fn=1;
Fb=1;
Fl=1;
n0=1.519*10^(-3);
jb=0.08;
Dpidealdik=8*jb*ndik*((p2*(v^2))/2)*((n2/n0)^(-0.14));
Dpdik=Dpidealdik;
Aw= 0.0017;
Ww=m2/(p2*n2);
Wz=(Ww*v)^(0.5);
Dctl=0.0525;
nvw=(Ds*0.25-((Ds-Dctl)/2))/Pb;
Dpw=(2+0.6*nvw)*((p2*Wz^(2))/2);
Dpe=Dpdik*((nvw+ndik)/ndik);
Dpgovdebell=2*Dpe+(Nbc-1)*Dpdik+Nbc*Dpw;
end
Instead of trying to syms Hs Ht and diff() the formula, hardcode the diff() as follows:
F1 = -C0 * C2 / Hs^2 + C7 * (4.1 * Hs^3.1 + 5.1 * Hs^4.1 / Ht + 5.1 * C3 * Hs^4.1) - C8 * Ht^3.5 / Hs^2;
F2 = -C0 * C2 / Ht^2 - C7 * Hs^5.1 / Ht^2 + C8 * (3.5 * Ht^2.5 / Hs + 2.5 * Ht^1.5 + 3.5 * C3 * Ht^2.5);
I wrote your alternative code instead of
syms Hs Ht;
CT=C0*(C1+C2*((1/Hs)+(1/Ht)+C3))+C7*(Hs^4.1+((Hs^5.1)/Ht)+C3*(Hs^5.1))+C8*(((Ht^3.5)/Hs)+(Ht^2.5)+C3*(Ht^3.5));
F1=diff(CT,Hs);
F2=diff(CT,Ht);
but i got error..
Undefined function or variable "Hs".
Error in myProject (line 33)
F1 = -C0 * C2 / Hs^2 + C7 * (4.1 * Hs^3.1 + 5.1 * Hs^4.1 / Ht + 5.1 * C3 * Hs^4.1) - C8 * Ht^3.5 / Hs^2;
I think the program do not work without that define Syms Hs Ht variables... But if I define Syms variables then this is not compile on my main java program again..
by the way I be grateful to you for your interested..
Are you sure you want to be taking the jacobian of [F1;F2] when F1 and F2 are already partial derivatives? The jacobian of CT would be [F1;F2] or [F1 F2] already.
I suspect that what you need is
jf = @(Hs,Ht) [-C0 * C2 / Hs^2 + C7 * (4.1 * Hs^3.1 + 5.1 * Hs^4.1 / Ht + 5.1 * C3 * Hs^4.1) - C8 * Ht^3.5 / Hs^2;
-C0 * C2 / Ht^2 - C7 * Hs^5.1 / Ht^2 + C8 * (3.5 * Ht^2.5 / Hs + 2.5 * Ht^1.5 + 3.5 * C3 * Ht^2.5)];
Emre
Emre on 14 Feb 2013
Edited: Emre on 14 Feb 2013
I see but it doesn't work otherwise.Actually I want to only run this matlab function on my java program.But I think it not possible in any wise without define Syms variables and I have to use unknown variable for Diff() on my matlab function.I'm using textbox for each input parameter(Cp1,Cp2,n1..) so All parameters dinamic, I can not do without matlab-java so i don't know that how can find different solution..
Lastly, Can i use this function on Matlab GUI?
Are the diff() always with respect to Hs and Ht, and only the values used to create the C* variables are changed by the textbox? Or do you change which variables you are differentiating with respect to?
Emre
Emre on 15 Feb 2013
Edited: Emre on 15 Feb 2013
Actually important variables-- C0,C2,C7,C8
By the way i found a solution- Matlab Control API for java . It's very useful but it doesn't work with it without creating a new session every time I call proxy-thread.so When I run program it has to open new matlab session.in spite of everything It's last and only solution about this problem..
Emre "creating a new session every time" - I'm rusty on matlabcontrol, but I recall that is programmer-selectable.
Emre
Emre on 15 Feb 2013
Edited: Emre on 15 Feb 2013
i fixed this problem but i have trouble again. this time I'm using existing proxy but I want to close matlab window when I finish my program and also i want to hide matlab window when main java program begin.. I know Iwant alot.. :) but other way it does not look pleasing to the eye..

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!