I have an anonymous function A taking two arguments. I need to convert this function so it takes one argument, by changing the other argument to a constant.
For example having a function:
A = #(X, Y) X + Y;
I would like now to have:
B = #(Y) 3 + Y;
This seems to be a normal thing to do in mathematics, so I guess there is a way to do such thing in MATLAB. I cannot find the solution though.
The reason I need to do something like this is that I have a function that does some calculations on A, but also needs to solve problems when one of the A's arguments is constant. For example find a minimum of the A for X = 3.
You can use the same anonymous function and put X as 3 in it but if you want to create another anonymous function, here is how to do that:
A = #(X, Y) X + Y;
B = #(Y) A(3,Y); %Here you have put X=3
To verify:
>> A(3,4)
ans =
7
>> B(4)
ans =
7
Related
I am having trouble using fminsearch: getting the error that there were not enough input arguments for my function.
f = #(x1,x2,x3) x1.^2 + 3.*x2.^2 + 4.*x3.^2 - 2.*x1.*x2 + 5.*x1 + 3.*x2 + 2.*x3;
[x, val] = fminsearch(f,0)
Is there something wrong with my function? I keep getting errors anytime I want to use it as an input function with any other command.
I am having trouble using fminsearch [...]
Stop right there and think some more about the function you're trying to minimize.
Numerical optimization (which is what fminsearch does) is unnecessary, here. Your function is a quadratic function of vector x; in other words, its value at x can be expressed as
x^T A x + b^T x
where matrix A and vector b are defined as follows (using MATLAB notation):
A = [ 1 -1 0;
-1 3 0;
0 0 4]
and
b = [5 3 2].'
Because A is positive definite, your function has one and only one minimum, which can be computed in MATLAB with
x_sol = -0.5 * A \ b;
Now, if you're curious about the cause of the error you ran into, have a look at fuesika's answer; but do without fminsearch whenever you can.
It is exactly what Matlab is telling you: your function expects three arguments. You are passing only one.
Instead of
[x, val] = fminsearch(f,0)
you should call it like
[x, val] = fminsearch(f,[0,0,0])
since you define the function f to accept a three dimensional vector as input only.
You can read more about the specification of fminsearch in the online documentation at http://mathworks.com/help/matlab/ref/fminsearch.html:
x = fminsearch(fun,x0) starts at the point x0 and returns a value x
that is a local minimizer of the function described in fun. x0 can be
a scalar, vector, or matrix. fun is a function_handle.
I am trying to create a for loop to run through my program and call out a function program I have created. I have been successful in getting it to run but now I would like to simplify it one more step and shorten my program by defining y as a vector of two functions instead of y1 and y2. I tried creating a 2 cell array with the functions inside but this is a failed effort. Here is what I have.
f =cell(size(a));
f(1) =[#(t) 4*t];
f(2) =[#(t) 20+(5-t).^2];
Any advice on organizing these 2 into a vector for a single input of y?
What if you use a cell array instead:
f =cell(2,1);
f(1) ={#(t) 4*t}; %// Note the curly braces
f(2) ={#(t) 20+(5-t).^2};
OR using the following, which is more intuitive using cell arrays (thanks #rayryeng!). Assign the content of the cell with curly braces instead of doing the opposite as above.
f{1} =#(t) 4*t;
f{2} =#(t) 20+(5-t).^2;
celldisp(f)
f{1} =
#(t)4*t
f{2} =
#(t)20+(5-t).^2
Another possibility would be to define a vector-valued function whose components are your two functions:
f1 = #(t) 4*t;
f2 = #(t) 20+(5-t).^2;
f = #(t) [f1(t) f2(t)];
So for example
>> f(2.5)
>> ans =
10.0000 26.2500
I need help to add a function, from 1 to 10, using MATLAB. The function is ((1/n)*sin(n*pi*x)) where n goes from 1 to 10 and x stays as a variable. Ultimately I want to have a summation of ten sines (i.e K1*sin(pi*x)+K2*sin(2*pi*x)+k3*sin(3*pi*x)+...etc) where k is a constant. I would really appreciate any assistance. Thanks
Edit: Thanks to everyone who helped with my problem However I should have been more specific when asking my question. After getting the summation, I want to plot the sine series. I tried doing this but I kept getting an error saying that "conversion from sym to double is not possible" Now I tried doing a for loop to get my graph. My code is as follows:
n = 0:10;
while i <= n
for i = 1:length(n);
T = (1/n(i))*sin(n(i)*pi*x);
end
i = 1+i;
max = sum(T);
end
plot(x,max,'black')
However this doesn't work. I don't think that this is the proper way to get the sum of a double. I would really appreciate it if someone could help me again. Thanks again
Learn to exploit MATLAB's vector nature.
For one-off shots:
>> f = #(n,x) sin((1:n)*pi*x) * (1./(1:n).');
>> f(200, 0.5)
ans =
7.828982258896381e-001
To be able to evaluate f(n,x) with vector/matrix input x:
>> f = #(n,x) reshape( sin( bsxfun(#times, (1:n)*pi, x(:)) ) * (1./(1:n).'), size(x) );
>> f(15,rand(2))
ans =
5.077194963950054e-001 2.660834723822258e-001
1.416130930552744e+000 1.012255979042172e-001
Replace (1./(1:n).') by [K1 K2 K3 ...].' when you want to use other constants than 1/n.
From my understanding you are trying to sum the multivariable expression. In your case, you have two variables n and x. You want to sum the expression from n = 1 to 10 keeping x as a variable. You can do this in MATLAB by using symsum function, whose syntax is
symsum(expr,var,a,b)
Where expression expr defines the terms of a series, with respect to the symbolic variable var. The value of the variable var changes from a to b. If you do not specify the variable, symsum uses the default variable determined by symvar. If expr is a constant, then the default variable is x.
So in your case
expr = (1/n*sin(n*pi*x)
var = n
a = 1
b = 10
The simple code would be
>>syms n x
>>F = symsum ((1/sym('n'))*sin(sym('n')*pi*x), n, 1, 10)
Answer to Edit: MATLAB cannot convert the sys variable to double. You can instead substitute the value of sym variable with variable from MATLAB workspace. For example you can plot the above function for the range 0 to 10 by using following command.
>> x = 0:0.1:10;
>> plot(x, subs(F))
This question already has answers here:
What is the # operator (at sign) in MATLAB?
(3 answers)
Closed 9 years ago.
I cannot find this syntax anywhere
y = #(beta, x)x*beta;
where x is some vector or matrix lets say. Is # used to reference another function so you can have more than one externally visiuble function in the same .m file? Sorry I'm new to matlab but can't find this in the documentation
That is the way to define an anonymous function in Matlab. It is basically the same as
function result = y(beta, x)
result = x * beta;
but without needing an m-file or subfunction to define it. They can be constructed within other m-files or even within an expression. Typical use is as a throw-away function inside the call of some complex function that needs a function as one of its inputs, i.e.:
>> arrayfun(#(x) x^2, 1:10)
ans =
1 4 9 16 25 36 49 64 81 100
I personally use them a lot to refactor a list of repetitive statements
a = complex_expression(x, y, z, 1)
b = complex_expression(x, y, z, 3)
c = complex_expression(x, y, z, 8)
into
f = #(n) complex_expression(x, y, z, n)
a = f(1)
b = f(3)
c = f(8)
More info from the Mathworks. They are more or less the same as a lambda expression in Python.
Think of # as anonymous which means an unnamed function (technically, in MATLAB, you must give it a name since you cannot do e.g., (#(x, y) x + y)(1, 2), that name is whatever variable you assign the anonymous function to).
The syntax #(x, y) x + y reads: create an anonymous # function with parameters x and y and return the result of the evaluation of the expression following the next closing parenthesis. In this case, that's the addition of x and y.
One thing that almost no one I know who uses MATLAB uses on a regular basis and instead uses repmat hell instead is bsxfun (it stands for binary singleton expansion).
With the expression bsxfun(#plus, randn(1000, 20), randn(1000, 1)) you essentially repmat the right hand side 20 times to form a new matrix that you then pass to the #plus function handle. You can pass any function around this way. Take a look at cellfun and arrayfun. They are incredibly useful.
For example, if I define the following in Matlab,
f(x) = x^2 + 3x - 2
How do I find what value the function evaluates to when x = 3?
Create a file called f.m that contains:
function y = f(x)
y = x^2 + 3x - 2
then in the command window, type f(3) and you should get back ans=16
See documentation on creating functions in MATLAB: http://www.mathworks.com/help/techdoc/ref/function.html
You can also use polyval(p,x) where p = [1 3 -2] in your example (the coeffients of your polynomial in descending order) and you will get the value of the polynomial at that point x (in your case, you would pass in 3).