필터 지우기
필터 지우기

Matlab functions - summation

조회 수: 1 (최근 30일)
Michael
Michael 2015년 11월 28일
답변: Arnab Sen 2015년 12월 30일
Hey,
I'm a beginner in Matlab.
I try at the moment to transfer a code which was written in Mathematica. There, I have many different functions w(k), e(k), f(k), etc. and another function X(w(k),e(k),f(k)). Let's say for simplicity that this function X(w(k),e(k),f(k)) does the following:
X(w(k),e(k),f(k))=Sum(w(k),k)+Sum(e(k)*f(k))
I tried a simpler version of this as follows:
function [wk,ek,S] = myfun(U,n,J,a)
ek = 2*J*(1-cos(k*a));
wk = sqrt(e(k)*(e(k)+ 2*U*n));
S = Sum??
end
The problem now is that I cannot even calculate ek and wk because I have to give Matlab a value of k? Does anyone know a possible to implement this?

답변 (1개)

Arnab Sen
Arnab Sen 2015년 12월 30일
My understanding is that you would like to have some alternative for symbolic math in 'Mathematica' for function call.
From the code you have provided I notice that you try to store the reference of the function into a variable what in MATLAB is called 'function handle'. You can do this by creating 'anonymous function' and store their handle to the variable.
The following code illustrates this approach:
function [wk,ek,S] = myfun(U,n,J,a)
k=2;
ek = @(k)2*J*(1-cos(k*a));
ek(k);
wk = @(k)sqrt(ek(k)*(ek(k)+ 2*U*n));
wk(k)
S =@(k)ek(k)+wk(k);
S(k)
end
Now, you do not need to provide the value of 'k' to execute the function. For more details about 'anonymous function' and 'function handle' refer to the following links:

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by