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.
Related
I am just getting started with MATLAB and I have written a function to produce the binomial expansion of (x-a)^n when given x, a and n. As far as I can tell my code should work, but I do not seem to be using the function variables correctly.
function expand(a,n,x)
f = 0;
for k = 0:1:n
f = f + nchoosek(n,k).*x.^(n-k).*(-a).^k;
end
end
I need to be able to call the function and have it output f as the expanded polynomial in x, for example calling expand(1,3,x) should return x^3-3*x^2+3*x-1, but instead, calling it gives this error:
Unrecognized function or variable 'x'. It seems like it wants me to call the function with x being another number but I in fact need it to be able to be any letter to be used as the variable in the polynomial.
I know in Maple I would specify the variable type in the function to be x::name so I'm assuming there's something similar in MATLAB that I don't yet know.
Thanks for any help.
There are two ways to go about this:
Create x as a symbolic variable. For example, syms x; expand(a,n,x). This gives you the power of using the symbolic toolbox features like simplify(), but comes with a bit of a performance penalty. You should avoid using the symbolic toolbox in intensive calculations.
Return an anonymous function, f=#(X)sum(arrayfun(#(k)nchoosek....,1:n). This has better performance and does not require double(subs(...)) when you want an actual numeric value, but may be too difficult to implement for a beginner.
gr = 9.81; %gravity
syms phi(t) m l
theta=1/3*m*l^2;
phidot=diff(phi,t);
U=m*gr*l/2*cos(phi);
T=1/2*theta*phidot^2+(1/2*phidot*l)^2*m;
L=T-U;
frst=diff(L,phidot);
The code is shown above. As you can see that phi(t) is symbolic time dependent function and phidot is derivation of it(also time dependent). L is obtained using these symbolic functions.
So, The problem is I can't derive L in terms of phidot in Matlab. The error occurs as following:
Error using sym/diff (line 26)
All arguments, except for the first one, must not be **symbolic** functions.
Error in pndlm (line 11)
frst=diff(L,phidot)
Is there any way to derive symbolic function in terms of another symbolic function? If not, Can you suggest me another alternative for avoiding this kind of error?
Possible duplicate of this
If you want to differentiate L with respect to q, q must be a
variable. You can use subs to replace it with a function and calculate
d/dt(dL/dq') later.
Remember those derivatives are partial. Hence just include explicitly the variable and obtain the expression.
% Variables
syms q qt m l g
theta=1/3*m*l^2;
% Lagrangian
U=m*g*l/2*cos(q);
T=1/2*theta*qt^2+(1/2*qt*l)^2*m;
L=T-U;
% Partial Derivatives
dLdq=diff(L,q)
dLdqt=diff(L,qt)
syms qf(t)
% Time Derivatives
qtf(t)=diff(qf,t)
dLdqf=subs(dLdqt,qt,qtf)
% Solution
m=1;l=1;g=9.81;
dsolve(diff(dLdqf,t)-dLdqf==0)
I experienced a strange behaviour in Matlab with the function subs and the built-in besselj:
syms x
f = besselj(1,x)
subs(f,1)
returns
besselj(1, 1)
even though the documentation states
"subs(s,new) returns a copy of s replacing all occurrences of the default variable in s with new, and then evaluating s.
The default variable is defined by symvar.
So I would expect the output of subs(f,1) to be 0.4401. eval(subs(f,1)) produces the correct output of 0.4401. Does anyone know why this is the case?
I feel like you're trying to define an anonymous function, you don't need subs or eval here, simply use f as an actual function...
% No need to use symbolic math toolbox for x or f
f = #(x) besselj(1,x); % Define f as an anonymous function of x
f(1) % Evaulate f at a given point, say x = 1
>> ans = 0.4401
Side note: If for some reason you're really set on using symbolic variables (they seem overkill here) then you may just want to use the eval function and a symbolic function handle instead of subs.
syms f(x) % Defines f as a symfun and x as a sym variable
f(x) = besselj(1,x); % Define the function
eval(f(1)) % Evaluate at x=1
>> ans = 0.4401
In answer to your question about why subs doesn't "evaluate" the answer when using subs(f, 1)... It's probably because the nature of the besselj function. Because you're using symvars, you are using the symbolic math package's besselj function (as opposed to the core-package function by the same name).
This means that a symbolic expression is displayed to the command window when subs is used, in the same way that any other symbolic expression is displayed without needing to be evaluated - i.e. they can be simplified but don't run other functions.
This is why you then need to run eval, to evaluate the simplified symbolic expression.
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)
I have made a function in func.m which is defined as follows:
function f = func(b)
f = b^2+1;
end
now i wish to differentiate with respect to that function i.e.
find 2*b+1 with a given value for b (lets say 1)
I tried diff(func(1)) but it returned an empty matrix.
Any ideas?
thanks
Your input argument func(1) is a vector of size 1x1. The function diff caled on vectors is this one: here. If you want to use the symbolic one, which allows to differentiate a function but requires the symbolic toolbox, you have to use a symbolic input. Explained here
Example:
% create a symbolic variable x
syms x
%differentiate f(x) and name it f1(x)
f1(x)=diff(f(x))
%get the value at point 1
f1(1)
If you don't have the symbolic toolbox, there is also a numeric solution.