Is it possible to have MATLAB do implicit differentiation with its symbols?
I have the following code
syms x;
y = symfun(sym('y(x)'), sym('x'));
yPrime = symfun(sym('y+(2*x)-1'), [sym('x'), sym('y')]);
diff(yPrime, x, 1)
From this I get
ans(x, y) =
2
but what I want to get (in some form) is
ans(x, y) =
dy/dx + 2
Is that possible?
It looks you're trying to use y as both a symbolic variable and an abstract symbolic function (symfun), which isn't possible. A symbolic function can only take symbolic variables as arguments. I think that you can accomplish what you want with (old-style strings not needed)
syms y(x) % Implicitly defines x too
yPrime = y+2*x-1 % Also a symfun because y is a symfun
diff(yPrime,x)
which returns this symfun
ans(x) =
diff(y(x), x) + 2
I don't know how to calculate the integral of the sum of function handles in a cell. Please see the below examples:
f{1} = #(x) x;
f{2} = #(x) x^2;
g = #(x) sum(cellfun(#(y) y(x), f));
integral(#(x) exp(g), -3,3);
Error: Input function must return 'double' or 'single' values. Found 'function_handle'.
PS: please don't change the formula, because this is just an example. My real problem is far more complicated than this. It has log and exp of this sum (integral(log(sum), -inf, inf)). So I can't break them up to do the integral individually and sum the integrals.I need to use sum(cellfun). Thank you.
Version: Matlab R2012a
Can anyone help me? Really appreciate.
You cannot add function handles, so anything that tries f{1}+f{2}+... would give an error.
But you can compute the integral of the sums like this, evaluating the function values one at a time and adding up the results:
function cellsum
f{1} = #(x) x;
f{2} = #(x) x.^2;
integral(#(x)addfcn(f,x), -3, 3)
end
function s = addfcn(f,x)
s = zeros(size(x));
for k = 1:length(f)
s = s + f{k}(x);
end
end
Note that x will usually be a vector when the integral command calls your functions with it. So your function definitions should be vectorized, .i.e., x.^2 instead of x^2, etc.
This looks really simple. I want to define a function:
syms x
f = x^2
I want to be able to do f(4) and it spits out 16. I also want to avoid having to write a new m-file.
When dealing with symbolic variables, to substitute in a numeric value, use subs(), i.e. symbolic substitution:
syms x
f = x^2
subs(f,4)
>> f = #(x) x^2;
>> f(4)
ans =
16
I'm trying to use fmincon in MATLAB and not quite sure what the heck the problem is. My function is:
function f = myfun(x4)
f = (C * x4 - d) .^ 2;
end
and I call it like this:
lb = zeros(3, 1);
x0 = [10; 10; 10];
[x4, fvalx4, exitflagx4, outputx4, lambdax4] = fmincon(#myfun,x0,[],[],[],[],lb,[]);
but when I run it I get
??? Error using ==> mtimes
Inner matrix dimensions must agree.
However, I checked and C is 112x3, and d is 112x1, and x4 is not initialized at all because that's what I'm trying to solve for. If I create a "dummy" x4 I can run
(C * x4 - d) .^ 2
without a problem.
Any thoughts? I realize this is the same as using lsqr or rather lsqlin with a lb of 0, which is also equivalent to lsqnonneg, but that's the point: I'm trying to verify results from those methods in different ways.
There are several issues here.
First, you need to learn how to pass in those parameters that are not optimized. The function myfun cannot see C and d, as functions have their own workspaces, unless they are nested functions.
Next, since you are computing a linear least squares subject to constraints, you need to return a sum of squares. fmincon needs a scalar objective. It does not understand what to do when you return a vector.
Personally, I would not even bother defining an explicit m-file. Define this using a function handle. So perhaps you might try this objective:
myfun = #(x4) norm(C*x4 - d);
lb = zeros(3, 1);
x0 = [10; 10; 10];
[x4, fvalx4, exitflagx4, outputx4, lambdax4] = fmincon(myfun,x0,[],[],[],[],lb,[]);
The function handle can see the value of C and d, so once the handle is created, those variables are carried along inside the function handle workspace.
Notice that I used norm to compute the sum of squares, in fact, here a sqrt of a sum of squares. But where the sum of squares is minimized, then so is the sqrt of that number. Its a bit cleaner looking with norm.
Another option is to use a version of myfun that explicitly passes in the value of C and d. Here I've given myfun arguments, then essentially created a function handle wrapper around myfun. You could have done that with an m-file version of myfun too.
myfun = #(x4,C,d) norm(C*x4 - d);
lb = zeros(3, 1);
x0 = [10; 10; 10];
[x4, fvalx4, exitflagx4, outputx4, lambdax4] = fmincon(#(x4) myfun(x4,C,d),x0,[],[],[],[],lb,[]);
The function that you pass to fmincon should return a scalar value. In your function it returns a 112-by-1 vector. Maybe you need to change
f = (C * x4 - d) .^ 2;
to
f = sum((C * x4 - d) .^ 2);
Say if i define the following:
g = #(x) x/sqrt(x^2+1)
How do i get the derivative function for g, which i can then use to evaluate at different points?
I tried the symbolic math toolkit, and tried the following:
>> syms x
>> f = x/sqrt(x^2+1)
f =
x/(x^2 + 1)^(1/2)
>> diff(f)
ans =
1/(x^2 + 1)^(1/2) - x^2/(x^2 + 1)^(3/2)
However, i cannot figure out how to turn this into a function handle/evaluate at different points. However, i prefer doing differentiation on function_handle.
Thank you very much!
Jason
You can use matlabFunction to convert a symbolic equation to a function. For example:
syms x1 x2;
f1 = x1^2+x2^2;
Df1 = jacobian(f1, [x1 x2]);
Df1 = matlabFunction(Df1);
Then Df1(0, 0) returns [0 0] as expected.
The function matlabFunction was introduced in version 5.2 (R2009a) of the Symbolic Math Toolbox.