multi-variable function, sum over one variable

I have a function of three variables N(z,t,n). I'd like to sum over only integers of n such that I am left with N(z,t), where z and t can be arrays of any length.
I've tried:
NN = 1:2000;
sum_function = @(z,t) sum(f(z,t,NN));
where f is a previously defined function of @(z,t,n), but this still requires that z and t have the same size as NN.
How do I evaluate this sum properly?

답변 (1개)

Voss
Voss 2022년 5월 3일
Maybe something like this
z = 1:3;
t = 1:4;
NN = 1:2000;
% Evaluate f(z,t,n) for each element of NN,
% storing all results in cell array C
C = arrayfun(@(n)f(z,t,n),NN,'uni',0);
% Inspect the first and last elements of C
disp(C{1}); disp(C{end});
1 2 3 4 2 4 6 8 3 6 9 12 2000 4000 6000 8000 4000 8000 12000 16000 6000 12000 18000 24000
% Concatenate all elements of C along the 3rd dimension
% and then sum that 3d matrix along the 3rd dimension.
% This will work as long as all elements of C are the
% same size; whether that's true depends on the
% definition of f.
result = sum(cat(3,C{:}),3)
result = 3×4
2001000 4002000 6003000 8004000 4002000 8004000 12006000 16008000 6003000 12006000 18009000 24012000
function out = f(z,t,n)
% Some function where z and t are vectors, n is a scalar.
% In this case, out is a matrix of size numel(z)-by-numel(t),
% but out can be any size as long as its size only depends on
% the sizes of z and t.
out = z(:).*t(:).'.*n;
end

댓글 수: 4

I would use a variation on that:
sum_function = @(z,t) sum(arrayfun(@(n)f(z,t,n),NN));
That'll work if f returns a scalar
z = 1:3;
t = 1:4;
NN = 1:2000;
sum_function = @(z,t) sum(arrayfun(@(n)f_scalar(z,t,n),NN));
sum_function(z,t)
ans = 2001000
But not otherwise
sum_function = @(z,t) sum(arrayfun(@(n)f_non_scalar(z,t,n),NN));
sum_function(z,t)
Error using arrayfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.

Error in solution (line 6)
sum_function = @(z,t) sum(arrayfun(@(n)f_non_scalar(z,t,n),NN));
[Even then, the outputs from f have to be the same size for all n (and if that's not the case then the question doesn't make sense - at least, not to me).]
Of course, without knowing what f is, it's hard to say how general the solution needs to be.
function out = f_scalar(z,t,n)
out = n;
end
function out = f_non_scalar(z,t,n)
out = n*ones(1,10);
end
To elaborate, f is the solution of a differential equation I solved analytically with a Fourier series
The issue I am running into is that when I use sum, matlab is summing over z or t instead of n. I'm trying to keep my result as a function handle since I need this function to solve another differential equation, but get an error when trying to plot f(z,t) while keeping one of z/t constant, e.g.
plot(z,f(z,0))
% Error: Vectors must be the same length.
which leads me to believe the wrong variable is being summed over.
The second parameter to sum() can be the dimension to sum over.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

질문:

2022년 5월 3일

댓글:

2022년 5월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by