how to calculate the summation of a function having two variables.

21 ビュー (過去 30 日間)
mohammad zahiri
mohammad zahiri 2019 年 7 月 7 日
回答済み: Star Strider 2019 年 7 月 7 日
I want to calculate:
in Matlab and I have f(x,y),(for example: f(x,y) = 2y*sin(x), N =50) how can I calculate f(x) in Matlab? I need it for minimizing f(x) with fminunc command.

回答 (1 件)

Star Strider
Star Strider 2019 年 7 月 7 日
The summation is across ‘f(x,y)’, not only ‘y’, so the function must work for all values of the arguments. The only way to do this is to create both arguments as matrices, since the multiplication will only work for matrices of the same size, then sum across the ‘y’ matrix.
One approach:
N = 50;
x = linspace(0, 10*pi, 250); % Define ‘x’
[X,Y] = ndgrid(x, (1:N)); % Define (x,y) As Matrices
fxy = @(x,y) sum(2*y.*sin(x),2); % Define ‘f(x,y)’, Sum Across Rows (‘y’)
fx = @(x) fxy(x,Y); % Define ‘f(x)’ Given ‘y’
plot(X, fx(X))
grid
The summation is across the rows because that is how the ndgrid function creates the matrices, as I have defined them here.
Even if this is homework, the approach would not be obvious for someone with little experience in these sorts of MATLAB calculations.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by