Filter löschen
Filter löschen

Double summation and surface plot?

1 Ansicht (letzte 30 Tage)
Solarmew
Solarmew am 25 Mai 2015
Kommentiert: Walter Roberson am 26 Feb. 2017
Could someone please show me the right way to do what I'm trying to do? Below is the description.
I have a function f that takes in 5 arguments z,x,y,n,m. The order of events should go as follows:
1. Upon calling the function test, the variable z is assigned, say z = 1.
2. A linear combination is created by adding f with with each of elements of ni inserted and the result is stored in fn, as such (z = 1, so no more z variable):
fn = x + 2.*y + exp(0) - sqrt(m) +
x + 2.*y + exp(2) - sqrt(m) +
x + 2.*y + exp(4) - sqrt(m) +
x + 2.*y + exp(6) - sqrt(m) +
x + 2.*y + exp(8) - sqrt(m) +
x + 2.*y + exp(10) - sqrt(m) =
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(m) =
6*x + 12*y + 25473.8 - 6*sqrt(m)
3. A linear combination is created by adding fn with each of elements of mi inserted and the result is stored in fnm. (I don't know how to do 1. and 2. simultaneously. If you do, please let me know):
fnm = 6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(0) +
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(2) +
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(4) +
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(6) +
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(8) +
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(10) =
6*x + 12*y + 25453.1
4. A surface is plotted by plugging in x and y from arrays xi and yi into fnm.
Here is my code:
function test(z)
f = @(z,x,y,n,m) z.*x + 2.*y + exp(n) - sqrt(m);
function s(z)
ni = 0:2:10;
mi = 0:2:10;
xi = -5:5;
yi = -5:5;
fn = @(n) arrayfun(@(z, x, y, ni, m) sum(f(z, x, y, ni, m)),n);
fnm = @(m) arrayfun(@(x, y, mi) sum(fn(x, y, mi)),m);
zz = sym(fnm);
[xx,yy] = meshgrid(xi,yi);
surf(xx,yy,zz)
end
s
end
I can't figure out why I'm getting so many errors.
Error using test2>@(x,y,mi)sum(fn(x,y,mi)) (line 11)
Not enough input arguments.
Error in test2>@(m)arrayfun(@(x,y,mi)sum(fn(x,y,mi)),m) (line 11)
fnm = @(m) arrayfun(@(x, y, mi) sum(fn(x, y, mi)),m);
Error in sym>funchandle2ref (line 1209)
S = x(S{:});
Error in sym>tomupad (line 1114)
x = funchandle2ref(x);
Error in sym (line 151)
S.s = tomupad(x);
Error in test2/s (line 12)
zz = sym(fnm);
Error in test2 (line 18)
s

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 25 Mai 2015
Bearbeitet: Walter Roberson am 26 Feb. 2017
Why are you trying to take sym(fnm) ? sym() invokes the symbolic toolbox to convert the given input to a symbolic variable. Even if you succeeded, the result would be a symbolic variable, and your later line
surf(xx,yy,zz)
would be attempting to use that symbolic variable in the third position of surf() where a numeric variable is expected.
What I suggest you do use use ndgrid
[x, y, n, m] = ndgrid(xi, yi, ni, mi);
allz = f(z, x, y, n, m);
[xx, yy] = ndgrid(xi, yi);
zz = sum(sum(allz,4),3);
surf(xx, yy, zz);
  2 Kommentare
zhuoran dong
zhuoran dong am 25 Feb. 2017
I run this code but it turns out that 'Error using surf (line 82) Data dimensions must agree'
Walter Roberson
Walter Roberson am 26 Feb. 2017
It works fine when I test it:
>> ni = 0:2:10;
mi = 0:2:10;
xi = -5:5;
yi = -5:5;
>> f = @(z,x,y,n,m) z.*x + 2.*y + exp(n) - sqrt(m);
>> z = 1
z =
1
>> [x, y, n, m] = ndgrid(xi, yi, ni, mi);
allz = f(z, x, y, n, m);
>> size(allz)
ans =
11 11 6 6
>> zz = sum(sum(allz,4),3);
>> size(zz)
ans =
11 11
>> [xx, yy] = ndgrid(xi, yi);
>> size(xx)
ans =
11 11
>> surf(xx, yy, zz);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by