formation of a function like in Mathematica

Hello, I am new user in Matlab, My question is that how can i form a function in editor file like we can make in Mathematica like
F[n_] := ComplexExpand[Im[etp[n]/(et[n] - (x + y I))]]
Also Is there any equivalent command for
ComplexExpand
in Matlab? Thanks

1 件のコメント

hparacha777
hparacha777 2015 年 8 月 6 日
編集済み: hparacha777 2015 年 8 月 6 日
Hi, you can write the function in Matlab Editor as:
function [out1, out2, ...] = myfun(in1, in2, ...)
Just save the file with the name of the function. Hope it helps.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 8 月 5 日

0 投票

f = @(n) ComplexExpand(imag(etp(n)/(et(n) - (x + y * i))))
however, ComplexExpand, etp, and et are not defined for MATLAB.
Mathematica's ComplexExpand is a non-trivial symbolic process so to get it right you would need to be working with the Symbolic Toolbox. At the moment I do not see an equivalent routine in the Symbolic Toolbox. In the programming language Maple that the Symbolic Toolbox has many similarities to, the routine would be named evalc(), but MATLAB'S evalc() has nothing to do with that.

7 件のコメント

Kashif
Kashif 2015 年 8 月 5 日
Ok thanks for reply, I am still facing problem regarding ComplexExpand, Also please tell How can i find partial derivative of above function with respect to both variables in Matlab
Walter Roberson
Walter Roberson 2015 年 8 月 5 日
After you have found the complex expansion (somehow), use gradient()
As you are taking the imaginary part of the function anyhow, you can probably substitute
syms etp etn x y real
syms f(n)
f(n) = simplify(imag(etp(n)/(et(n) - (x + y * i))));
gradient(f(n), [x, y])
Kashif
Kashif 2015 年 8 月 6 日
Thanks for the reply, in my problem etp and etn are known values and these are not variables,
% Actually In Mathematica
% With the following function
% F[n_] := ComplexExpand[Im[etp[n]/(et[n] - (x + y I))]]
% n1 = n/8;
% n2 = n + n/8;
% et(n1)=8.33663 + 6.8417*1i;
% etp(n1)=-4.67008 + 10.1188*1i;
% et(n2)=0.641945 - 0.526831*1i;
% etp(n2)=-1.00014 - 0.25351*1i;
% I am getting like following values of F(n1) and F(n2)
% F[n1]=116.308/((8.33663 - x)^2 + (6.8417 - y)^2) - (
% 10.1188 x)/((8.33663 - x)^2 + (6.8417 - y)^2) - (
% 4.67008 y)/((8.33663 - x)^2 + (6.8417 - y)^2);
% And
% F[n2]=-(0.689644/((0.641945 - x)^2 + (-0.526831 - y)^2)) + (
% 0.25351 x)/((0.641945 - x)^2 + (-0.526831 - y)^2) - (
% 1.00014 y)/((0.641945 - x)^2 + (-0.526831 - y)^2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% I am looking for the same function in Matlab, which may give me like
% same values of f(n1) and f(n2) in Matlab, But with following
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms x y real
syms f(n)
f(n) = simplify(imag(etp(n))/((et(n)) - (x + y * 1i)));
gradient(f(n), [x, y])
% with your command i am not getting the same values as above
Walter Roberson
Walter Roberson 2015 年 8 月 6 日
I am not positive this will work but try
syms et(n)
n1 = n/8;
n2 = n + n/8;
feval(symengine,'_assign', et(n1), 8.33663 + 6.8417*1i);
feval(symengine,'_assign', et(n2), 0.641945 - 0.526831*1i);
then see what
et(n1), et(n2), et(n)
return
If not then.... ummm, maybe
syms et n
evalin(symengine, 'et := N -> piecewise([N=n/8, 8.33663 + 6.8417*1i], [N=n+n/8, 0.641945 - 0.526831*1i], [Otherwise, procname(N)]);');
A bit tricky here is the unassigned behaviour for et at values other than n/8 and n+n/8.
You asked for the partial derivative with respect to both variables. I interpreted that as gradient. Perhaps you wanted
diff(f(n),x,y)
Kashif
Kashif 2015 年 8 月 6 日
編集済み: Kashif 2015 年 8 月 6 日
Thanks, But my problem is not with et or etp, as these are known in my program, but only problem that i need function defining and ComplexExpand like command in matlab to get the same expressions as above.
Walter Roberson
Walter Roberson 2015 年 8 月 6 日
You are not likely to get those any time soon. MATLAB has a limited interface subset to the Symbolic Toolbox ('MuPAD') which is more powerful.
I was assisting you under the assumption that you wanted to make something work. The primary barrier to making something work is the definition of et and etp as objects that need to recognize two specific inputs and return specific arbitrary outputs for those two inputs while being meaningfully defined symbolically for the formula with indefinite n.
Kashif
Kashif 2015 年 8 月 8 日
ok Sir , Thanks a lot for your time, i will try it

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeArduino Hardware についてさらに検索

質問済み:

2015 年 8 月 5 日

コメント済み:

2015 年 8 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by