Edit
To clarify. I have a function f
that takes in 5 arguments z,x,y,n,m
. The order of events should go as follows:
Upon calling the function
test
, the variablez
is assigned, sayz = 1
.A linear combination is created by adding
f
with with each of elements ofni
inserted and the result is stored infn
, 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)
- A linear combination is created by adding
fn
with each of elements ofmi
inserted and the result is stored infnm
. (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
- A surface is plotted by plugging in
x
andy
from arraysxi
andyi
intofnm
and of Edit
I'm having trouble with the double summation over the function f
. I tried to follow the example in the last answer presented here, but it's not working for some reason. Because my variables x
and y
are not defined beforehand, I included them into @()
in the arrayfun
. I was first getting the error Z must be a matrix, not a scalar or vector.
, so I changed the function handle fnm
to a symbolic function after reading about it here. But now the whole thing exploded ... I don't know what's going on. Why is it saying there aren't enough inputs? fn
should only be a function of x, y, m
on line 11 since z
is already defined to be some number (lets say 1) and n
's just got summed over.
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
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