I'm having some problems with my code. Here is it:
syms I1(t) I2(t)
eqn1 = diff(I1(t),t) == 12-2*I1(t)+diff(I2(t),t);
eqn2 = diff(I2(t),t) == 1/3*(diff(I1(t),t)-4);
eqns = [egn1; eqn2]
cond1=I1(0)==0;
cond2=I2(0)==0;
conds=[cond1; cond2];
[I1Sol(t), I2Sol(t)]=dsolve(eqns, conds)
When i solve, i get
??? Error using ==> syms at 61
Not a valid variable name.
I don't know how to fix it. I'm new to Matlab so i hope to someone to help me. Thank you so much.
try now, and make sure you add folder contain your script to matlab "add path"
clear
clc
syms I1(t) I2(t)
eqn1 = diff(I1(t),t) == 12-2*I1(t)+diff(I2(t),t);
eqn2 = diff(I2(t),t) == 1/3*(diff(I1(t),t)-4);
eqns = [eqn1; eqn2]
cond1=I1(0)==0;
cond2=I2(0)==0;
conds=[cond1; cond2];
[I1Sol(t), I2Sol(t)]=dsolve(eqns, conds)
I got this result
eqns =
diff(I1(t), t) == diff(I2(t), t) - 2*I1(t) + 12
diff(I2(t), t) == diff(I1(t), t)/3 - 4/3
I1Sol(t) =
3*exp(-3*t)*((16*exp(3*t))/9 - 16/9)
I2Sol(t) =
exp(-3*t)*((16*exp(3*t))/9 - 16/9) - (4*t)/3
Related
>> syms x v(x) w(x);
>> eq1 = 2*v + 3*w == 4;
>> eq2 = 5*v + 4*w == 3;
>> sol = solve([eq1,eq2],[v,w])
I tried to implement this code in MATLAB, but error flashes out as "The second argument must be a vector of symbolic variables." I have tried similar things in Python using SymPy, but never such error comes. How to correct out this?
Have a look at the help file for the multivariate case of solve and the example in the help file
openExample('symbolic/SolveMultivariateEquationsAndAssignOutputsToStructureExample')
Applied to your problem
syms v w;
eq1 = [2*v + 3*w == 4;5*v + 4*w == 3];
sol = solve(eq1)
sol.v
sol.w
but if you just want to solve for v w then you could use e.g.
[2 3;5 4]\[4;3]
I'm fairly new to Matlab and programming in general, and this error message has defeated me 😅.
The simplified code is the following:
Lpi = .05;
Dpi = .01;
LpD = [.1, 06];
LpI = [.9, 14];
DpR = [.4, 06];
DpI = [.6, 08];
IpH = [01, 30];
syms H(t) L(t) D(t) R(t) I(t)
HtL =#(t) H(t) * (Lpi*L(t) + Dpi*D(t));
LtD =#(t) LpD(1)*HtL(t-LpD(2));
LtI =#(t) LpI(1)*HtL(t-LpI(2));
DtR =#(t) DpR(1)*LtD(t-DpR(2));
DtI =#(t) DpI(1)*LtD(t-DpI(2));
ItH =#(t) IpH(1)*(LtI(t-IpH(2))+DtI(t-IpH(2)));
odea = diff(H, t) == ItH(t) - HtL(t);
odeb = diff(L, t) == HtL(t) - LtD(t) - LtI(t);
odec = diff(D, t) == LtD(t) - DtR(t) - DtI(t);
oded = diff(R, t) == DtR(t);
odee = diff(I, t) == LtI(t) + DtI(t) - ItH(t);
odes = [odea;odeb;odec;oded;odee];
S = dsolve(odes);
If there are any doubts about the purpose or operation, I can try to explain them below.
Once the differential equations are declared, it is my understanding that they indeed are differential equations, as Matlab's workspace shows them to be of the form:
val(t) =
diff(H(t), t) == (24*H(t - 44)*(D(t - 44)/100 + L(t - 44)/20))/25 - H(t)*(D(t)/100 + L(t)/20)
(this is the case of odea, I don't include the rest as they are similar but longer)
The problem pops up when I call dsolve, as it displays the following error:
Error using mupadengine/feval_internal (line 172)
Expecting an ODE in the specified variable.
Error in dsolve>mupadDsolve (line 328)
T = feval_internal(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 183)
sol = mupadDsolve(args, options);
Error in main (line 50)
S = dsolve(odes);
I am very confused by this error, as it is, as I understand, textbook usage of the function dsolve (according to MathWorks' "Solve a System of Differential Equations" page).
Thank you in advance for any help.
Hi I am very new to MATLAB. I was trying to solve these equations to either get an analytical solution or solve them numerically. For the analytical solution, I get the following error:
Warning: Cannot solve symbolically. Returning a numeric approximation instead.
In solve (line 305)
Here is my code:
syms A B Ph Pl
delta = 0.1;
mu = 0.02;
sigma = 0.2;
w = 1;
k = 3;
l = 2;
beta = (0.5 - mu/sigma^2) + ((mu/sigma^2 - 0.5)^2 + 2*delta/sigma^2)^0.5;
alpha = -((0.5 - mu/sigma^2) - ((mu/sigma^2 - 0.5)^2 + 2*delta/sigma^2)^0.5);
eqn1 = (A*(Ph^(-alpha)) + (Ph/delta-mu)) -(B*Ph^beta)-k;
eqn2 = (A*Pl^(-alpha) + Pl/(delta-mu) -w/delta) - B*Pl^beta + l;
eqn3 = -alpha*A*(Ph^(-alpha-1)) + 1/(delta-mu) - (beta*B*Ph^(beta-1));
eqn4 = alpha*A*Pl^(-alpha-1)- (beta*B*Pl^(beta-1));
sol = solve([eqn1==0, eqn2==0, eqn3==0, eqn4==0], [A, B, Ph, Pl]);
Matlab is telling you it can't find an analytic solution, but it is definitely finding numerical solutions when I run it, however, they're all complex. Type:
sol.A
in your command window to see what A looks like, same with B, Ph and Pl.
I get an error when taking the gradient of a symbolic function. Can anyone tell me why I get this error?
syms x1 x2 R
L0=0; %[m]
k1=8; %[N/m]
k2=4; %[N/m]
F1=5; %[N]
F2=10; %[N]
F = 0.5*k1*(sqrt(x1^2 + (L0-x2)^2) - L0)^2 + 0.5*k2*(sqrt(x1^2 + (L0+x2)^2)
- L0)^2 - F1*x1 - F2*x2;
f = matlabFunction(F);
R = 0.1;
GI = x1^2 + x2^2 - R^2;
gi = matlabFunction(GI);
epsilon=0.001;
xo=[0,0]';
k = 0;
r = (sqrt(5)-1) / 2;
rpe=0.01;
Merit_pe = #(x1,x2) f(x1,x2) + rpe*(max(0,gi(x1,x2)))^2;
g = gradient(Merit_pe, [x1 x2]);
Error:
Error using sym/max (line 97)
Input arguments must be convertible to
floating-point numbers.
Error in
Ex>#(x1,x2)f(x1,x2)+rpe*(max(0,gi(x1,x2)))^2
Error in sym>funchandle2ref (line 1249)
S = x(S{:});
Error in sym>tomupad (line 1154)
x = funchandle2ref(x);
Error in sym (line 163)
S.s = tomupad(x);
Error in sym/gradient (line 17)
args = privResolveArgs(sym(f));
Error in Ex (line 31)
g = gradient(Merit_pe, [x1 x2])
I think the max part is causing me trouble, but I still need to determine the gradient of this function. Any advise? (I could do it by hand I guess, but I'd rather not if I don't have to)
Directly taking the gradient of max(0,gi(x1,x2)) is not possible in matlab. Instead the function should be defined according to the following definition.
The function Merit_pe can then be defined as follows:
if gi(xo(1,1),xo(2,1)) > 0
Merit_pe = #(x1,x2) f(x1,x2) + rpe*(gi(x1,x2))^2;
else
Merit_pe = #(x1,x2) f(x1,x2);
end
The gradient can then be determined using:
g = gradient(Merit_pe, [x1 x2]);
I'm trying to solve a system of two non-linear equations in Matlab in a for loop. I am aware that the for loop may cause the program to run slow but right now it takes around an hour for one instance to be solved. On debugging the code, I realized that this line causes the program to hang:
[res,status] = mupadmex(statement);
If I terminate the program I get the following:
Operation terminated by user during mupadengine/evalin (line 97)
In mupadengine/feval (line 150)
[S,err] = evalin(engine,stmt);
In solve (line 160) sol = eng.feval('symobj::solvefull',eqns,vars);
In algorithm2 (line 139)
answer = solve (f , g);
where line 97 refers to the same line of code. Here's the section of my code where I call the solve function:
syms ro1 t_prime1
assume(t_prime1 > 0)
for i=1:1:length(n1ps)
n1p = n1ps(1,i);
n2p = n2ps(1,i);
w1 = (ro1/theta_p)*(1-exp((-1)*theta_p*t_prime1));
w2 = exp((-1)*theta_p*t_prime1);
w3 = (ro1/theta_p)*(1-exp((-1)*theta_p*T1))-q(1,1);
w4 = (1-exp((-1)*n1p*theta_p*T1))/(1-exp((-1)*theta_p*T1));
w5 = exp((-1)*theta_p*(T1-t_prime1));
Q1 = (w1 + w2*w3*w4)*w5 - q(1,1);
w6 = ((ro-ro1)/theta_p)*(1-exp((-1)*theta_p*(n1p*T1 + t_prime1 - n2p*T2)));
w7 = exp((-1)*theta_p*(n1p*T1 + t_prime1 - n2p*T2));
w8 = ((ro-ro1)/theta_p)*(1-exp((-1)*theta_p*T2))-q(1,2);
w9 = (1-exp((-1)*n2p*theta_p*T2))/(1-exp((-1)*theta_p*T2));
w10 = exp((-1)*theta_p*(T2-(n1p*T1 + t_prime1 - n2p*T2)));
Q2 = (w6 + w7*w8*w9)*w10 - q(1,2);
Q1_prime = q(1,1)*((exp((tN1 - n1p - 1)*theta_p*T1)-1)/(1-exp((-1)*theta_p*T1)));
Q2_prime = q(1,2)*((exp((tN2 - n2p - 1)*theta_p*T2)-1)/(1-exp((-1)*theta_p*T2)));
f=Q1_prime-Q1;
g=Q2_prime-Q2;
answer = solve (f , g);
end