MATLAB/Octave: feval with vector input - matlab

I want to put a function in feval(f,x) that has a vector as input
e.g.
function [ ret ] = f (x)
ret = x(1)^2 - x(2)^2;
end
and
x = [1,2]
but octave always gives an error code:
`x' undefined near line 6 column 18
evaluating argument list element number 1
evaluating argument list element number 1
It seems feval can only evaluate numbers and not vectors. Is there any way to do this?

Create a handle to your function and then call feval on the handle, passing it your vector as its argument:
h = #(x)myfun(x);
x = [1, 2];
y = feval(h, x);
I tried this out in Octave on your function and it seems to work.

Related

Series-function with vector-coefficients in Matlab

I have a vector v and would like to define a function x using following formular:
Now I try to define this in Matlab, but for some reason I can't access an array within a symsum.
v = [1 2 3 4];
syms k
x = #(t) symsum(v(k) * exp(2*pi*i*k*t/size(v)), k, 1, size(x_n));
When I try to evaluate this Function x(4) I get an exception:
Error using sym/subsindex (line 796)
Invalid indexing or function definition. When defining a function, ensure that the arguments are
symbolic variables and the body of the function is a SYM expression. When indexing, the input
must be numeric, logical, or ':'.
Do you know, why? Do you have a solution or workaround?

Calling function with 2 parameters on matrix of dimension n x 2

I have a function f that takes two parameters and want to apply the function on each row of a matrix M with dimension n x 2.
I have tried using, num2cell(M, 2) and then calling cellfun(#f, num2cell(M,2)). This doesn't work since f takes two arguments and not a vector.
But then calling cellfun(#f, num2cell(num2cell(M,2)(1))) resulted in the error:
error: binary operator '^' not implemented for 'cell' by 'scalar' operations
f looks as follows:
function y = f (r, phi)
y = r^(2/3)*sin(2*phi/3);
endfunction
Can someone tell me how to call the function on the matrix, where each row vector is automatically split into 2 scalars?

How to define a vector that its elements are created in a For Loop?

I want to define a vector,X, with five elements:
syms a
X = zeros(1,5)
X(1) = 1;
for k=1:4
X(k+1)=X(k)+a^2;
end
Actually I need to have the vector X, that its elements should be based on variable a. But I face an error in Matlab, when I write the code above:
The following error occurred converting from sym to double:
Error using symengine (line 58)
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use VPA.
Error in Code2 (line 5)
X(k+1)=X(k)*a^2;
How to fix this?
You are mixing between symbolic variables to doubles,
Doubles holds a value, you can use the loop that you wrote only if a is a double and already holds a value (the value can be the input of a function) .
for example :
function ret=testFunc(a)
X = zeros(1,5)
X(1) = 1;
for k=1:4
X(k+1)=X(k)+a^2;
end
ret=X
end
if you want to work with syms (for other symbolic analysis) you can define x as a symbol too, for example :
syms a x
x(1)=1;
for i=2:5
x(i)=x(i-1)+a.^2;
end
now, x is a function of a, and if you print x you will get :
[ 1, a^2 + 1, 2*a^2 + 1, 3*a^2 + 1, 4*a^2 + 1]
to evaluate the value of x, you will still need to input a value for a.
Matlab help suggest subs function for replacing a in wanted value as follows:
y = subs(x,a,4)
in that point y is still a symbol and you will need to make it a double by using
double(y)

Use fminsearch to find local minimizer and the minima at that value

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.

Evaluate Equation in Cell Array, MATLAB

I have a cell array containing functions (with function handle) and I want to evaluate these inside a for-loop. I want to evaluate the differential equations:
x1'= x2,
x2' = ax2-bx1
My code is like this:
init = [0,0];
F = {#(x1,x2) x2,#(x1,x2)(a*x2-b*x1)};
X0 = init;
for i=1:10
X = X0 + c*F(init(1),init(2));
X0 = X;
init[1] = {X(1)};
init[2] = {X(2)};
end
The constants a,b and c are given.
I get the error:
Subscript indices must either be real positive integers or logicals.
Can someone help me with this?
F is a 1x2 cell array. To access elements (in your case, function handles) within this array you must use the curly braces {} to do so. For example, the first function is retrieved by F{1} and the second by F{2} using positive integer indices.
In your example, init is a 1x2 array of zeros so when the code evaluates
F(init(1),init(2));
it is trying to access something within F using indices that are not positive. Hence the error.
I suspect that you will need to do something like the following in your for loop and evaluate each function separately
for i=1:10
for j=1:2
% get the jth function handle
func = F{j};
% evaluate
X(j) = X0(j) + c*func(init(1),init(2));
% save
end
end
I've left the save* portion to be filled in by you because it isn't all that clear to me why there are the different init,X0 and X variables - could you consolidate them?
Note also your mixture of [] and () brackets when accessing arrays. In MATLAB, if the array is of type cell, then we use the {} to access elements. For all other arrays, we use the () brackets.
First of all, there are two other problems with your code: c is not defined and you are trying to index into init with []-brackets, which will throw:
Error: File: foo Line: 8 Column: 8
Unbalanced or unexpected parenthesis or bracket.
The subscript error occurs because you are trying to access F(0,0) because init(1) and init(2) are 0. Remember that the way you declared F, it is a cell array:
>> F = {#(x1,x2) x2,#(x1,x2)(a*x2-b*x1)};
>> whos F
Name Size Bytes Class Attributes
F 1x2 288 cell
Hence, F(0,0) is illegal because indexes in matlab start with 1. Your functions reside in F{1} and F{2}.
>> F{1}
ans =
#(x1,x2)x2
>> F{2}
ans =
#(x1,x2)(a*x2-b*x1)
>> f = F{1}
f =
#(x1,x2)x2
>> f(0,0)
ans =
0