Error when solving symbolic system of ODEs using dsolve - matlab

I have been trying to use the command dsolve in Matlab to solve a set of ODEs, but I am getting these errors:
Error in dsolve>mupadDsolve (line 332)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 193)
sol = mupadDsolve(args, options);
Below is the code if someone wants to take a look at it:
syms t b1 b2 k1 k2;
A=0.5;
m1=3;m2=4;w=6;
y=A*sin(w*t);
xt=dsolve('m1*D2x1+b1*((Dx1)-Dy)+k1*(x1-y)+b2*((Dx1)-(Dx2))+k2*(x1-x2)=0','m2*D2x2+b2*((Dx2)-(Dx1))+k2*(x2- x1)=0','x1(0)=0','Dx1(0)=0','x2(0)=0','Dx2(0)=0');
Could someone please help me with that?
Thank you all very much

Quoting from the dsolve documentation:
The names of symbolic variables used in differential equations should not contain the letter D because dsolve assumes that D is a differential operator and any character immediately following D is a dependent variable.
As such, the Dy in your argument string is telling dsolve there is some unknown function y to solve for but without an initial condition (in addition to it being an indeterminant system).
To fix the issue, define the derivative a y outside dsolve and assign it to a variable without D:
syms t b1 b2 k1 k2;
A=0.5;
m1=3;m2=4;w=6;
y=A*sin(w*t);
dydt = diff(y,1);
xt=dsolve('m1*D2x1+b1*((Dx1)-dydt)+k1*(x1-y)+b2*((Dx1)-(Dx2))+k2*(x1-x2)=0','m2*D2x2+b2*((Dx2)-(Dx1))+k2*(x2-x1)=0','x1(0)=0','Dx1(0)=0','x2(0)=0','Dx2(0)=0');
This version of the code runs for me ... and runs and runs. While it does run, I think more constants need to be defined in order for an answer to be generated.

Related

solving nonlinear equations including max function

I want to solve this system of equations using MATLAB:
I solved the second equation and put it in the first one. Then I tried this:
syms v_star
eqn = max((1.8+0.81*v_star), (-1+0.9*v_star)) == v_star;
solx = solve(eqn,v_star)
And I get this error:
Error using symengine
Input arguments must be convertible to floating-point numbers.
How can I solve this system of equations?

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)

MATLAB symbolic equation with LUT

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

Roots of implicit function including integral of modified Bessel function - symbolic variables not compatible with fzero

I'm trying to solve equation f for r, as below:
syms rho
C0 = 0.5;
a_bar = sqrt(-log((1-C0)/(1+C0)));
l = 0.77;
f = #(r) exp(-r^2)*int(rho*exp(-rho^2)*besseli(0,2*r*rho),rho,0,a_bar)-(l-1)*int(rho*exp(-rho^2),rho,0,a_bar);
r1 = fzero(f,1);
However the symbolic output from the first integral (containing besseli) is giving errors in fzero. The problem seems to be that besseli contains rho (when I remove this instance of rho I don't get any errors).
I've tried playing with subs and eval, and solving the entire thing as symbolic, but it's really been trial and error to be honest. I'm sure there's something simple that I'm missing - any help would be amazing!
Cheers,
Alan
As suggested by David in the comments to my question, including double within the function solves this problem; i.e:
f = #(r) double(exp(-r^2)*int(rho*exp(-rho^2)*besseli(0,2*r*rho),rho,0,a_bar)-(l-1)*int(rho*exp(-rho^2),rho,0,a_bar));
r1 = fzero(f,1);
This works as it converts the symbolic expression (involving rho and r) into a numerical object. My equation doesn't have any roots but that's an issue with my working beforehand.
Thanks again to David and Mad Physicist for the help on this.

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.