solving system of equations

Tried to get this to work but have had no luck.
Main Code:
syms x K M c1 c2 c3 c4
x0 = [0,0,0,0];
y0 = [1,200,-80,900];
coeff = [1,-0.02,12.5,-0.058,27.77];
rx = -0.05*exp(0.005*x)*cos(11.7*x) + 0.07*exp(0.005*x)*sin(11.7*x);
y_new = Examprac(x0,y0,coeff,rx);
g = 
g = 
C = struct with fields:
M: -(40000000*cos((117*x)/10) - 56000000*sin((117*x)/10))/(1254221280*cos((117*x)/10) + 27288860249597*sin((117*x)/10)) K: 0
Output argument "y" (and possibly others) not assigned a value in the execution with "solution>Examprac" function.
Function:
function y = Examprac(x0,y0,coeff,rx)
syms x c1 c2 c3 c4 K M
yp = 2*K*cos((117*x)/10)*exp(x/200) + 2*M*sin((117*x)/10)*exp(x/200);
yp_der1 = diff(yp);
yp_der2 = diff(yp_der1);
yp_der3 = diff(yp_der2);
yp_der4 = diff(yp_der3);
yp_der1= simplify(yp_der1);
yp_der2= simplify(yp_der2);
yp_der3= simplify(yp_der3);
yp_der4= simplify(yp_der4);
g = simplify(coeff(1)*yp_der4 + coeff(2)*yp_der3 + coeff(3)*yp_der2 + coeff(4)*yp_der1 + coeff(5)*yp == rx)
g = coeff(1)*yp_der4 + coeff(2)*yp_der3 + coeff(3)*yp_der2 + coeff(4)*yp_der1 + coeff(5)*yp == rx
C = solve(g, [M, K])
end

 Accepted Answer

Torsten
Torsten on 19 Dec 2023
Edited: Torsten on 19 Dec 2023
I wonder what you expect as solution for 1 equation with 7 unset parameters.
Say you have the equation
g = x + c1 + c2 + c3 + c4 + K + M == 0
What would you like to see as output from the command
C = solve(g,[M,K])
?
One possible solution is
K = 0, M = -(x + c1 + c2 + c3 + c4)
, and that's what the symbolic toolbox offers to you.
What are the arrays x0 and y0 good for in your code ?

5 Comments

Sean Rotmansky
Sean Rotmansky on 19 Dec 2023
Edited: Sean Rotmansky on 19 Dec 2023
Hello!
x0 and y0 arrays are for the larger code this is supposed to feed into (I have to solve for yp and yh then y= yp+yh) so they are not used here. The only unknown parameters (or at least, that is how it should be) are K, M and x. I have completed code with a similar question where g = 180*M*sin(x) + 180*K*cos(x) == 90*sin(x),
and it solved for both M and K (I think one was zero and the other was 0.5). I am just super lost as to why this one doesn't work.
The expression you get for g is of the form
%a*cos(y) + b*K*cos(y) + c*M*cos(y) + b*M*sin(y) = c*K*sin(y) + d*sin(y)
In order to get a solution for K and M that is independent of x, the following linear system in K and M must hold:
%(a+b*K+c*M) = 0
%(b*M-c*K-d) = 0
Now solve for K and M.
Here is a possible code:
syms x c1 c2 c3 c4 K M xx
coeff = [1,-0.02,12.5,-0.058,27.77];
rx = -0.05*exp(0.005*x)*cos(11.7*x) + 0.07*exp(0.005*x)*sin(11.7*x);
yp = 2*K*cos((117*x)/10)*exp(x/200) + 2*M*sin((117*x)/10)*exp(x/200);
yp_der1 = diff(yp);
yp_der2 = diff(yp_der1);
yp_der3 = diff(yp_der2);
yp_der4 = diff(yp_der3);
yp_der1= simplify(yp_der1);
yp_der2= simplify(yp_der2);
yp_der3= simplify(yp_der3);
yp_der4= simplify(yp_der4);
g = simplify(coeff(1)*yp_der4 + coeff(2)*yp_der3 + coeff(3)*yp_der2 + coeff(4)*yp_der1 + coeff(5)*yp == rx)
g = 
eqn1 = subs(g,x,0)
eqn1 = 
eqn2 = subs(g,x,10/117*pi/2)
eqn2 = 
[Knum,Mnum] = solve([eqn1,eqn2],[K,M])
Knum = 
Mnum = 
simplify(subs(g,[K,M],[Knum,Mnum]))
ans = 
symtrue
last question,
do you know why g would assume that form? I derive the left side of the equation 4 times, then multiply each derivative with a coefficient. The form should look like a bunch of M*cos()*e^() and K*cos()*e^() terms, and the right side should have stayed like this:
rx = -0.05*exp(0.005*x)*cos(11.7*x) + 0.07*exp(0.005*x)*sin(11.7*x);
Is there another function or method you would recommend?
Torsten
Torsten on 19 Dec 2023
Edited: Torsten on 19 Dec 2023
You can trace how the derivatives of yp are computed by removing the semicolon behind the lines.
But it is obvious that differentiating expressions of the form
a1*sin(b*x)*exp(c*x)+a2*cos(b*x)*exp(c*x)
will give expressions of the same form, only with the values for a1 and a2 changed.
Thank you so much for your help! I finally have a number answer for M and K.

Sign in to comment.

More Answers (0)

Products

Release

R2023b

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!