Help with MATLAB symbolic toolbox

조회 수: 3 (최근 30일)
Scott Banks
Scott Banks 2025년 1월 25일
답변: Scott Banks 2025년 1월 26일
Hi there everyone,
I need some help with using the MATLAB symbolic tool box.
I have some symbolic expressions to manipulate and I am getting quite frustrated.
I have the paticular solution to a differential equation and need to find the coefficients C1 and C2.
% Set up symbolic variables
syms t C1 C2
% Set up parameters
K = 2.2167e+06;
M = 45.96;
wn = sqrt(K/M)
c = 0.5
% set up quadratic terms
a = 1
b = 2*c*wn
c = wn^2
% Solve for u
u1 = (-b + sqrt(b^2 - 4*a*c))/2
u2 = (-b - sqrt(b^2 - 4*a*c))/2
% Particalular solution to differential equation
yp = vpa(exp(-43.1357*t)*(C1*cos(74.7132*t) + C2*sin(74.7132*t)) == 0,4)
% The first derivitive of yp
dyp = vpa(diff(yp),4)
% Find the value of C1 interm of C2
C1 = vpa(solve(yp, C1),4)
% Sub C1 into equation dyp to have an expression in terms of C2 alone
eq = vpa(subs(dyp,C2,C1),4)
As you can see I obtain the value of C1 in terms of C2. I then want to plug this into equation "dyp". However, when I run the code for "eq", C1 is still in the expression. The expression should contain just C2's, and this is really frustrating. If equation "eq" contains just C2 values I can then solve for C2 - I hope this makes sense.
Is there a way around this. I have not very good at plug and chucking with long and messy equatons, so I am hoping MATLAB can help me out.
Thanks in advance,
Scott

채택된 답변

Torsten
Torsten 2025년 1월 25일
편집: Torsten 2025년 1월 25일
Can't you use "dsolve" ?
If not, use
% Find the value of C1 interm of C2
C1_sol = vpa(solve(yp, C1),4)
% Sub C1 into equation dyp to have an expression in terms of C2 alone
eq = vpa(subs(dyp,C1,C1_sol),4)

추가 답변 (2개)

Paul
Paul 2025년 1월 25일
Hi Scott,
See below for solution
% Set up symbolic variables
syms t C1 C2
% Set up parameters
K = 2.2167e+06;
M = 45.96;
wn = sqrt(K/M);
c = 0.5;
% set up quadratic terms
a = 1;
b = 2*c*wn;
c = wn^2;
% Solve for u
u1 = (-b + sqrt(b^2 - 4*a*c))/2;
u2 = (-b - sqrt(b^2 - 4*a*c))/2;
% Particalular solution to differential equation
yp = vpa(exp(-43.1357*t)*(C1*cos(74.7132*t) + C2*sin(74.7132*t)) == 0,4)
yp = 
% The first derivitive of yp
dyp = vpa(diff(yp),4)
dyp = 
% Find the value of C1 interm of C2
C1 = vpa(solve(yp, C1),4)
C1 = 
To substitute the current value workspace variables into an expression call subs with the expression to be updated as the lone argument
% Sub C1 into equation dyp to have an expression in terms of C2 alone
% eq = vpa(subs(dyp,C2,C1),4)
eq = vpa(subs(dyp),4)
eq = 
  댓글 수: 1
Walter Roberson
Walter Roberson 2025년 1월 25일
% Set up symbolic variables
syms t C1 C2
Q = @(v) sym(v);
% Set up parameters
K = Q(22167)*Q(10)^2;
M = Q(4596)/Q(10)^2;
wn = sqrt(K/M);
c = Q(0.5);
% set up quadratic terms
a = Q(1);
b = 2*c*wn;
c = wn^2;
% Solve for u
u1 = (-b + sqrt(b^2 - 4*a*c))/2;
u2 = (-b - sqrt(b^2 - 4*a*c))/2;
% Particalular solution to differential equation
F1 = Q(431357)/Q(10)^4;
F2 = Q(747132)/Q(10)^4;
yp = exp(-F1*t)*(C1*cos(F2*t) + C2*sin(F2*t)) == 0;
% The first derivitive of yp
dyp = diff(yp)
dyp = 
% Find the value of C1 interm of C2
C1 = solve(yp, C1)
C1 = 

댓글을 달려면 로그인하십시오.


Scott Banks
Scott Banks 2025년 1월 26일
Thank you, guys, for all your help!

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by