My question refers to the Matlab symbolic toolbox.
I'm trying to derive a symbolic function that is a function of another symbolic function. Say that I have a function x that is an unspecified function x=x(y(theta)). I'd like to take the derivative of x with respect to theta: dx/dtheta=dx/dy * dy/dtheta
In Matlab I write
syms theta y(theta);
x=sym('x(y(theta))');
diff(x,theta)
The answer I get is 0. I really cannot figure out what is wrong with the code.
Any help is greatly appreciated. Thanks!
Derivating f(g(y)):
syms x,y
f = symfun(sym('f(x)'), [x])
g = symfun(sym('g(y)'), [y])
diff(f(g(y)),y)
Related
I have an equation like this:
(5+x)^2/15+(x-4)^2/10=100
Can MATLAB solve this equation directly, without having access to the symbolic toolbox? If it can not do this, how can I resolve this problem?
It's possible, but requires some hand work.
Your function is a polynomial:
x^2/6 - (2*x)/15 + 49/15 = 100
When pulling the 100 to the left hand side, we can find the roots:
roots([1/6 -2/15 -1451/15])
ans =
24.4948
-23.6948
where the argument is specified as the prefactors in decreasing order of power.
Code with which I found the polynomial (requires the Symbolic Math toolbox):
syms x
fun = (5+x)^2/15+(x-4)^2/10-100;
f = simplify(fun);
How about using an anonymous function:
f=#(x)(5+x)^2/15+(x-4)^2/10-100;
X0=1; % initial guess
x_out=fzero(f,X0);
Hello and thanks for reading this:
This question is regarding Matlab:
I need integrate this expression of two variables
w1=subs(diff(K,Y1),{Y1,Y2},{0.2,0.3})
where K is defined as a handle function:
K=#(X1,X2,Y1,Y2)...
so w1 is a ''function'' of two variables
but Matlab says that
''Undefined function or method 'matlabfunction' for input arguments of
type 'sym' ''
If I ask for K or w1
which w1
Matlab returns
''w1 is a variable''
When I use dblquad the error message is
''If FUN is a MATLAB object, it must have an feval method.''
I know that Matlab is right but how can I obtain the (double) integrate of w1 in the unit square?
I have tried a lot of things but I don't get it.
Can anyone help me?
I'm not completely sure which variables in your code are defined as symbolic, but here's a minimal example.
Suppose I have a function handle of the kind:
x = #(t, a) t*a*a;
If I define:
syms t a;
I can get a symbolic derivative while substituting a value like in your code:
f = subs(diff(x(t, a), a), a, 3);
Note that f is now a symbolic variable. To convert this variable into a function handle you can use matlabFunction like so:
fun = matlabFunction(f);
Hope this helps.
Let
syms h
g=exp(h)+h*exp(h)+h^2
so, the coefficients of g in respect to his given by the function coeffs:
coeffs(g,h)
and it returns:
[exp(h),exp(h),1]
It's like this function deals with the symbolic expression like a polynomial in h.
The problem is that this function doesnt return the zero coefficients, so if I have an
g=h*exp(h)+h^2
the function returns only:
[exp(h), 1].
But what I need is:
[0,exp(h), 1]
So, what can I do here?
I went to a lot of topics on SO and the solution is:
syms h
g=h*exp(h)+h^2
m = eval(feval(symengine,'coeff',g,h,'All'));
I hate to be one of those posters that lectures, but this is listed right in the Matlab help (help coeff) or online
coeffs(g,h, 'All')
Say I want to find the value of a stieltjes integral f(x) dg(x) from a to b. In other words, integrate f(x) with respect to g(x). I know the variable and function values and I'm looking for a numerical result.
Is there a standard function in Matlab that does this? I've been calculating it somewhat manually by rectangle method, would any Matlab function be faster and/or more accurate?
I haven't had much experience with Matlab, and I can't find the solution in the documentation. Any help would be appreciated! :)
There is no function to support this but if you have the derivative of either functions you can use quad (or other members of quad family). If you have the derivative of g(x) then
integral(a,b) f(x) dg(x) = integral(a,b) f(x) g'(x) dx [[if g'(x) is bounded ]]
and if you have the derivative of f(x) you can use integration by parts to get
integral(a,b) f(x) g'(x) dx = f(b)g(b) - f(a)g(a) - integral(a,b) f'(x) g(x) dx
Is it possible to get the derivative of a function_handle as a other function_handle?
Like:
fun1 = #(x) x^2;
% do that ...
disp(fun2);
#(x) x*2
I know how to find the derivative of a symbolic function but I can't convert a function_handle to a symbolic function.
I'm new to matlab and I couldn't find any way on doing that. Thanks in advance.
The short answer is "No." MATLAB has no idea what the contents of the function_handle mean in a symbolic sense. You're better off creating it using syms in first place.
A longer answer would be either to use the Symbolic Math Toolbox, as suggested by #A Danesh, or an approximation, as suggested by #Andrey.
However, if you're always working with polynomials, then you can store the coefficients as an array and use the following functions:
polyval to evaluate
conv to multiply
deconv to divide
polyder to differentiate
polyint to integrate
syms x
f = #(x) x^2 + 1;
diff(f,x)
Ans:
2*x + 1
You can get an approximation function:
delta = 0.0001;
d = #(x)( (fun1(x+delta) - fun1(x))./delta)
you can't analytically from a function handle.
but if you got the symbolic math toolbox you can derivate the symbolic function and create a function handle from the result.