How to store a function in a variable in MATLAB? - matlab

Let's say I want to store the function y(x) = x + 2 in a variable.
Is there any way to save y = x + 2, and access as y(x)? For example, y(2)?

An anonymous function is what you're looking for:
>> y = #(x) x+2;
>> y(2)
ans =
4

Related

Summation of cell array of function handles

I have a cell array with size (n,1) that includes a function handle. Every cell has to include specific function handle and the summation of function handles in the previous cells. How can I perform this operation? To clarify this is an illustration.
A = cell(size(ones(n,1)));
for i = 1 : n
A{i,1} = #(x) A{i-1,1} + i .* x;
end
How to get A{n,1} at x = 2 (for example)
You are actually pretty close, but you need to add a special case for i = 1 and you need to call the function:
n = 10;
A = cell(size(ones(n,1)));
A{1,1} = #(x) 1 .* x;
for ii = 2 : n
A{ii,1} = #(x) A{ii-1,1}(x) + ii .* x;
end
I replaced i with ii, to avoid confusion with complex numbers. For the case n = 10:
>> A{n}(2)
ans =
110
Recalling #gnovice comment, you can also just make a cell array of the handles, and then call a function that sums them up to n:
N = 10;
A = cell(N,1);
A{1} = #(x) 1.*x;
for k = 2:N
A{k} = #(x) k.*x;
end
% the following function sums the output of A{1}(x) to A{n}(x):
f = #(n,x) sum(cellfun(#(c) c(x),A(1:n)));
The result:
>> f(5,2)
ans =
30
>> f(N,2)
ans =
110
This way, every change of the functions in A will have an immediate effect upon redefining f:
>> A{3} = #(x) -x;
>> f = #(n,x) sum(cellfun(#(c) c(x),A(1:n)));
>> f(N,2)
ans =
102
>> f(5,2)
ans =
22

Matlab: how to pass a list through a function, return list of answers

This should be pretty easy, but I'm new to matlab so forgive me.
I'm making a function function that takes a function y = "some function of x" as an input. y could be a function handle ( let's say y= #(x) x^2 ), or I can y be a symbolic expression (like y = x^2)... Whatever is easier.
I want to run the x list through the function y, and return a list of the calculated y values. so the result should be [1 4 9 16 25]. How would I do this in a function function?
It should look something like this:
function myfunc = func(f)
xlist = [1 2 3 4 5]; %IMPORTANT: in this case, xlist's class is "sym"
ylist = ... %statement of something like "f(xlist)" goes here*
You can make y as a function handle and call in the m file itself.....
y = #(x) x.^2 ;
x = [1 2 3 4 5] ;
y = y(x)
If you want to make it a function...either define y inside the function or make it as a input as shown below:
function out = myfun(x,y)
if ~isa(y,'function_handle')
error('input t should be a function handle')
end
out = y(x) ;
end
y = #(x) x.^2 ;
x = [1 2 3 4 5] ;
out = myfun(x,y)

How to create a matrix of functions dynamically

I want to create a matrix of functions, however I'd like to dynamically generate it. For example:
myMatrix = zeros(3);
test = #(x) x*y;
for ii = 1:3
myMatrix(ii) = test(ii);
end
something like that to generate: #(y) [y, 2*y, 3*y]
I do not have access to the sym library.
You can't make a matrix of functions, but you can make cell of function handles, e.g.
cellOfFunctions = {};
for i = 1:3
cellOfFunctions{end + 1} = #(y) y*i;
end
Then you can get each handle as follows (for the first function handle):
fh1 = cellOfFunctions{1};
Then execute it with y = 3:
result = fh1(3);
Depending on your purposes, you can make a single function which generates the matrix you have in your example:
>> f = #(y) bsxfun(#times, 1:3, y(:));
>> f(2:5)
ans =
2 4 6
3 6 9
4 8 12
5 10 15

Sending a function into a matlab function

I'm trying build a matlab function that will evaluate a function and vector that are sent in as parameters. I'm having a hard time trying to figure out how to send in the function so that it can be evaluated in the matlab function. I figured out how to do it without the function but I'm a little lost trying to evaluate it within a matlab function. Below is my code...
This is what I'm trying to do...
x = [x1 x2]';
f = x(x1)^2 + 2 * (x2)^2
x = [5 10];
f = (5)^2 + 2 * (10)^2 % which I would like to return 225, not a column vector
This is what I have and what I have tried...
x = [5 10]';
% without using a matlab function
% k = 1
% f = x(k)^2 + 2 * x(k + 1)^2; % returns the correct answer of 225
f = x^2 + 2 * x^2 % complains about the scalar 2
f = x.^2 + 2 * x.^2 % returns a column vector [75; 300]
function [value] = evalFunction(f,x)
value = f(x);
I've tried...
f = #(x) x.^2 + 2 * (x+1).^2;
value = evalFunction(#f,x) %Error: "f" was previously used as a variable
So I tried...
f = #(x) x.^2 + 2 * (x+1).^2;
value = evalFunction(f,x) %value = [97;342]
I'm new to matlab so any help is appreciated. I've been doing some research and found some stuff here on stackoverflow but can't seem to get it to work. I've seen there are other ways to do this, but I will eventually be adding more code to the matlab evalFunction function so I'd like to do it this way. Thanks!
Anonymous functions and function handles plus array indexing. Taking x as a 2-element vector, define and use your function like:
f = #(x) x(1).^2 + 2 * x(2).^2;
value = evalFunction(f,x) % but you can just do f(x) if that is all you need
However, if evalFunction does nothing other than evaluate f at x, then you don't need it at all. Just do f(x).
Alternately,
f = #(x1,x2) x1.^2 + 2*x2.^2;
value = evalFunction(f,x1,x2); % here your function will call it by f(x1,x2)
You are probably coming at this from a C background - in Matlab, x+1 is the entire vector x with 1 added - not the element offset by 1.
The function you need is
f = #(x)x(1).^2 + 2 * (x(2)).^2;
or, to be a little more "matlab-like":
f = #(x) [1 2] * x(1:2)'.^2;
Which performs the element-wise square of the first two elements of x as a column vector, and then does the matrix multiplication with [1 2], resulting in
1 * x(1) .^2 + 2 * x(2) .^2;
Which seems to be what you were asking for.
caveat: did not have opportunity to test this...

How to plot this equation in matlab

alright well I have the follow function
y=sin(x)^2 + [(10+2x+x^2) / (5+2x^2)]
i need to plot it on the interval y = -2 to y = 2 so how would I set that up?
I did this in matlab
>> y = sin(x).^2 + (10 + 2*x + x.^2)/(5+2*x.^2)
>> x = -2:0.01:2;
is that a correct setup? Or have I done something wrong
You need to declare a variable before you use it. In this case, x doesn't depend on y, so declare it first. In addition, there is a ./ operator missing.
x = -2:0.01:2;
y=sin(x).^2 + (10+2*x+x.^2) ./ (5+2*x.^2);
plot(x,y)
f = #(x) sin(x)^2 + [(10+2*x+x^2) / (5+2*x^2)];
ezplot(f)