Questions about how to plot a summation function with two variables

1 visualización (últimos 30 días)
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.

Respuesta aceptada

James Tursa
James Tursa el 21 de Dic. de 2020
Editada: James Tursa el 21 de Dic. de 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 comentarios
James Tursa
James Tursa el 21 de Dic. de 2020
Editada: James Tursa el 21 de Dic. de 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.
Yao Zhao
Yao Zhao el 21 de Dic. de 2020
Thank you very much, James. This problem is solved!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by