필터 지우기
필터 지우기

Transfer Code from Mathematica to Matlab

조회 수: 4 (최근 30일)
Theon
Theon 2016년 1월 8일
댓글: Walter Roberson 2020년 4월 23일
I'm trying to transfer the following code from Mathematica to Matlab but I'm having trouble. Can anyone help?
n[x_, z_, T_] = 1/(Exp[x/T]*z + 1)
f[x_] = (3/2)*Sqrt[x]
eqchem[z_, T_] =
1 == Integrate[f[x]*n[x, z, T], {x, 0, \[Infinity]}, Assumptions -> {T > 0}]
zSolution[T_] := z /. FindRoot[Evaluate[eqchem[z, T]], {z, .01}]
points = Prepend[Table[{T, -T Log[zSolution[T]]}, {T, 0.1, 5, 0.1}], {0., 1.}] // Re;

채택된 답변

Walter Roberson
Walter Roberson 2016년 1월 9일
n = @(x, z, T) 1./(exp(x./T).*z + 1);
f = @(x) (3/2)*sqrt(x);
eqchem = @(z, T) integral(@(x) f(x)*n(x, z, T), 0, inf) - 1;
At this point the Mathematica code provided breaks down. It uses z without z being a parameter. We could add it as a parameter to zSolution, an assumption that it should have been written as
zSolution[T_, z_] := ....
but then we need to pass in two parameters in the reference to zSolution that appears in points. We can see that there is explicit range of T values being iterated over in the Table construction, but there is no match for z there. Is the table built to be symbolic in z? Or is the {0., 1.} intended to be a z range, or is that {0., 1.} intended to be a final (or leading) point in the table being constructed?
Perhaps I am misunderstanding the meaning of the zSolution code. The /. operator looks like you might have intended a rule replacement, but the right side of the ./ does not appear to be in the form of a Mathematica rule. Is the intention that zSolution should be the z such that z is a root of eqchem near 0.01 ?
  댓글 수: 5
Walter Roberson
Walter Roberson 2016년 1월 10일
zSolution = @(T) fzero(@(Z) -1 + integral(@(x) (3/2)*x^(1/2)/(exp(x/T)*Z+1), 0,inf), [eps(realmin),20]);
t = 0.1 : 0.1 : 5;
points = arrayfun(zSolution, t);
scatter([0,t], [1,points])
The 20 upper bound is arbitrary, needed to get the fzero to work for t = 5. The necessary upper bound goes up fairly quickly: for example by t = 17.87 you need slightly more than 100.
If you do not put in a range, just an initial point, then fzero will head to negatives and the integral will start to fail.
Theon
Theon 2016년 1월 10일
Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

mohammad hodaei
mohammad hodaei 2020년 4월 23일
But, I have codes around 28000 line. how do i tranfer it?
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 4월 23일
Mathematica has a function to translate Mathematica into MATLAB.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Robust Control Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by