MATLAB symbolic equation with LUT - matlab

I'm trying to solve an equation in MATLAB using the method of symbolic variables through the syms tool. The problem is in my equation i have a term which is function of the symbolic variable i'm trying to solve but it is not analitically defined. Instead, it is defined through a lookup table (that is a column array with my symbolic variable as index). When i try to add the term LUT(symbVariable) to my equation, i get this error:
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 ':'.
I suppose the problem is related with the bolded sentence. Anyone knows a way i could solve this problem? I'm not very used to matlab programming and as far i didn't find anything useful. Thank you in advantage.
Here's a sample code of what i'm trying to explain:
syms v % definition of symbolic variable v
eqn = myLUT(v) + v + v^2 == 0; % definition of my equation (this is the call that gives me the error)
res = solve(eqn,v,'Real',true); % solving the equation

Related

How can I derive symbolic function in MATLAB in terms of another symbolic function?

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)

Integrating a function on matlab

I have a density function f_N which is defined as follows (K_nu(z) is the modified Bessel function):
I want to compute the following integral for each value of N:
The following is the implementation of the above in matlab.
for N=1:100
syms z
f =#(z) (1/(gamma(N)*sqrt(pi))*(z/2).^(N-0.5).*besselk(0.5-N,z));
g = #(z) f(z).*log(f(z));
val=integral(g,0,Inf);
But when I run the above code, it's always returning NaN for varoious values of N with the following warning:
Warning: Infinite or Not-a-Number value encountered
Can someone suggest a simple way to do this or avoid this issue?
I don't think you are doing what you think you are doing. Your declaration of z as a symbol will get overridden by the function handle definition. That means the integral is not a symbolic one, but a numeric one. So what you simply need to do is to drop the function handle notation "#(z)" and do the integral symbolically...
My guess, without thoroughly analysing this, is that one of the points in you integration domain [0,inf] yields a value f (x)=inf, which would destroy a numeric integration technique, but perhaps not a symbolic one.

How to solve an equation with piecewise defined function in Matlab?

I have been working on solving some equation in a more complicated context. However, I want to illustrate my question through the following simple example.
Consider the following two functions:
function y=f1(x)
y=1-x;
end
function y=f2(x)
if x<0
y=0;
else
y=x;
end
end
I want to solve the following equation: f1(x)=f2(x). The code I used is:
syms x;
x=solve(f1(x)-f2(x));
And I got the following error:
??? Error using ==> sym.sym>notimplemented at 2621
Function 'lt' is not implemented for MuPAD symbolic objects.
Error in ==> sym.sym>sym.lt at 812
notimplemented('lt');
Error in ==> f2 at 3
if x<0
I know the error is because x is a symbolic variable and therefore I could not compare x with 0 in the piecewise function f2(x).
Is there a way to fix this and solve the equation?
First, make sure symbolic math is even the appropriate solution method for your problem. In many cases it isn't. Look at fzero and fsolve amongst many others. A symbolic method is only needed if, for example, you want a formula or if you need to ensure precision.
In such an old version of Matlab, you may want to break up your piecewise function into separate continuous functions and solve them separately:
syms x;
s1 = solve(1-x^2,x) % For x >= 0
s2 = solve(1-x,x) % For x < 0
Then you can either manually examine or numerically compare the outputs to determine if any or all of the solutions are valid for the chosen regime – something like this:
s = [s1(double(s1) >= 0);s2(double(s2) < 0)]
You can also take advantage of the heaviside function, which is available in much older versions.
syms x;
f1 = 1-x;
f2 = x*heaviside(x);
s = solve(f1-f2,x)
Yes, the Heaviside function is 0.5 at zero – this gives it the appropriate mathematical properties. You can shift it to compare values other than zero. This is a standard technique.
In Matlab R2012a+, you can take advantage of assumptions in addition to the normal relational operators. To add to #AlexB's comment, you should convert the output of any logical comparison to symbolic before using isAlways:
isAlways(sym(x<0))
In your case, x is obviously not "always" on one side or the other of zero, but you may still find this useful in other cases.
If you want to get deep into Matlab's symbolic math, you can create piecewise functions using MuPAD, which are accessible from Matlab – e.g., see my example here.

Converting symbolic equation to fitness function which can be evaluated by genetic algorithm

I have a symbolic equation and wish to convert to a function which can be evaluated by genetic algorithm (ga). I have tried using the matlabFunction and convert the symbolic equation into a matlab file. However, this generated file can only be evaluated by fmincon or patternsearch algorithms and not genetic algorithm. I get this error using ga.
Caused by: Failure in initial user-supplied fitness function evaluation. GA cannot continue.
It seems like the matlabFunction does not generate the format required by ga, can anyone please advise what's the solution/workaround to this problem?
The code are as follow:
N = 24;
X = sym('x',[2*N 1]);
Y = X(1:N);
W = 3.2516e-6.*Y.^3 - 0.0010074.*Y.^2 + 0.390950.*Y+2.2353;
Z = P.*W;
totR = sum(Z);
totR = subs(totR,[P],[price]);
matlabFunction(totR,'vars',{X},'file','objFcn');
% Call to ga
x1 = ga(#objFcn, N*2, A, b, Aeq, beq)
Thanks!
From the documentation for ga:
The fitness function should accept a row vector of length nvars and return a scalar value.
Your objFcn is accepts a column vector and throws an error if a row vector is passed in. You can fix this (I think) by changing this line to:
X = sym('x',[1 2*N]);
If P is non-scalar then you may need to transpose it. Of course, without runnable code there could be all sorts of other things going on too. There are probably other places and ways that you could fix the issue.

Calculating derivative and integration using matlab

I am trying to find second derivative of a function, but while initializing my symbols i am getting the following error:
Error using ==> subsindex
Function 'subsindex' is not defined for values of class 'sym'.
The commands I am using are:
syms x a b c L;
u = (a*x(x-L))+(b*x((x^2)-(L^2)))+(c*x((x^3)-(L^3)));
"u" is my function.
I don't know much about MATLAB's symbolic capabilities, but that error is coming from the pieces like
x(x-L)
which MATLAB is interpreting as an indexing operation. Did you mean multiplication there? I.e.
x*(x-L)