Questions about how to plot a summation function with two variables
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
James Tursa
le 21 Déc 2020
Modifié(e) : James Tursa
le 21 Déc 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 commentaires
James Tursa
le 21 Déc 2020
Modifié(e) : James Tursa
le 21 Déc 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Graphics Performance dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!