Questions about how to plot a summation function with two variables

1 回表示 (過去 30 日間)
Yao Zhao
Yao Zhao 2020 年 12 月 21 日
コメント済み: Yao Zhao 2020 年 12 月 21 日
Hello, I am trying to plot a summation function with two variables (x and y) shown in the figure. My code is:
[x,y] = meshgrid(1:1:40,1:1:5);
z=fun(x,y);
surf(x,y,z);
function [z] = fun(x,y)
z=0;
for i=1:x
z=z+(0.5.^((i-1)./x)-0.5.^(i./x)).*(1-(exp(y)-1)./(exp(y).*0.5.^(i./x)-0.5.^((i-1)./x)));
end
end
But the results are only valid for i=1, not for i=1:x. I can not find the errors, so I need help from all of you. Thanks in advance.

採用された回答

James Tursa
James Tursa 2020 年 12 月 21 日
編集済み: James Tursa 2020 年 12 月 21 日
From the formula image, it appears you need fun( ) to create a matrix z where each element corresponds to the formula for a particular x and y pair. E.g., since you are passing in matrices
function z = fun(X,Y)
z = zeros(size(X));
for x = X
for y = Y
% insert your for-loop here to update z(x,y)
end
end
  6 件のコメント
James Tursa
James Tursa 2020 年 12 月 21 日
編集済み: James Tursa 2020 年 12 月 21 日
Sorry. I forgot how for-loops act on matrices used for indexing (they do it by entire columns at once). Try this instead:
for m = 1:size(z,1)
for n = 1:size(z,2)
x = X(m,n);
y = Y(m,n);
for i=1:x
z(m,n) = z(m,n) + etc.
Yao Zhao
Yao Zhao 2020 年 12 月 21 日
Thank you very much, James. This problem is solved!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by