how can you pass values to variables in an an array of functions?

I am trying to make a movie of the solutions to the heat equation for various values of a parameter in the coefficients . I want to create an array for the fourier coefficients of the solution. How can I define an 'array function' ? does this idea exist.
apologies if theis is a duplicate question. couldn't find this answer anywhere.

6 Comments

What does "array function" mean? What have you tried already and which explicit problems occur?
I was puzzled, too, Jan. I can't envision why there would be a problem in doing what OP asks about with just "ordinary" arrays which are passed to functions by simply placing the array name in the argument list...
I could do this another way.... but i think its an interesting lesson to learn how to do..... and it is prettier i think..... lets see if I can produce the idea here.
a;
x = [a+2, a+3]
for i = 1:10
a = i
print x
end
maybe I am missing something simple. I don't code matlab or any other language. I have found that the only way an array or vector will hold a variable is if I define them syms (symbollicaly) ...
but it seems once i define a variable as syms, i cant give it any value.
by function array or vector I mean.... like a function f(x) = x^2 can receive a value for x is there a way to define an array like array(x) = [x^2, x^3] such that if I type array(2) it outputs [4,8]?
obviously x indexes the array, but is there a way to perform this idea?
Yes, I think you're right that you're missing the simple basis of arrays, functions and variables. A run through the initial tutorials will help a lot. For example you can write
x = 2;
x.^[2 3]
to compute your example. I suspect that using the symbolic toolbox is highly confusing at this stage - it's not needed for numerical computations.
what if the function which defined each cell was more complicated than an exponent?
so basically passing a value to a parameter (variable) in multiple cells in an array is something everyone should avoid?
@Leonard: If you have a more complicated function, you create a function file, which can be as complicated as you want.

Sign in to comment.

Answers (1)

Playing with anonymous functions in combination with cellfun
fh{1}=@(x) x.*x;
fh{2}=@(x) x.*x.*x;
fh{3}=@(x) x.*x.*x.*x;
fh{4}=@(x) sin(x);
fh{5}=@(x) cos(x);
foo = @(z) cellfun( @(f,x) f(x), fh, repmat({z},size(fh)), 'uni', false);
foo(2)
returns
ans =
[4] [8] [16] [0.9093] [-0.4161]
However, those are cell arrays. Matlab doesn't support ordinary arrays of anonymous functions.
.
Or a little better
foo = @(z) cellfun( @(f,x) f(x), fh, repmat({z},size(fh)), 'uni', true);
foo(2)
returns
ans =
4.0000 8.0000 16.0000 0.9093 -0.4161
which is an array of doubles.
.
I guess that what you want is a Functional programming languages

3 Comments

Thanks. I appreciate the guidance. This teaches me a little something. My design was unnecessary for sure. So, what I was describing to do would be something like this:
for n = 1:10
fh{n}=@x x.*x;
end
foo = @z cellfun(@(f,x) f(x), fh, repmat({z},size(fh)), 'uni', true);
for i = 1:5
for p = 0:size(foo)
coeffecientsarray(p)=foo(i)
end
end
But i don't think that makes sense at all to do. I should just make a single 1D array where every next n entries has the parameter take on the next new value.
perhaps this thread should be deleted so no one gets confused by my nonsense haha any votes on that idea?
An array with ten identical functions might not be useful.
However, anonymous functions are powerful and it takes some experiments to learn how to use them. "[...] any votes on that idea?" I vote for keeping the post. I contributed an experiment.
Yes, building the 10 anonymous functions is being overcomplicated. Almost certainly you can do what you want just by passing vector arguments to ordinary functions. I'd say keep the post - there are many less interesting and less useful threads here.

Sign in to comment.

Asked:

on 22 Mar 2014

Commented:

on 24 Mar 2014

Community Treasure Hunt

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

Start Hunting!