Questions about how to plot a summation function with two variables

1 view (last 30 days)
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.

Accepted Answer

James Tursa
James Tursa on 21 Dec 2020
Edited: James Tursa on 21 Dec 2020
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 Comments
James Tursa
James Tursa on 21 Dec 2020
Edited: James Tursa on 21 Dec 2020
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!