Solving equations with parameters and then inputting different values

18 次查看(过去 30 天)
I am solving the ODE representing the movment of a damped harmonic spring system.
I am currently solving the equation with the following code, where the parameters of the equation (m,c,k) being specific values before solving. My purpose is to calculate the maximum displacemnt and maximum acceleration during the movement.
My issue is, I need to solve the equation for many different combinations of parameters, and run time is extreamly high. Currently, i call this function every time with the specific numeric value of m,c,k. Is there any way to solve the equation parametrcly and only then input different values in the parameter?
function [maxDisplacment,maxAcceleration] = SystemTest(c,k,m,Tmax,x0,v0)
%% Analytic solving:
syms x(t)
eq = m*diff(x,t,t) + c*diff(x,t) + k*x == 0;
cond = [x(0) == x0, subs(diff(x(t), t), t, 0) == v0];
mov = dsolve(eq, cond);
vel = diff(mov,t);
acc = diff(mov,t,t);
%% For finiding max displacment:
% Find the critical points of velocity
critPointsX = solve(diff(mov, t), t);
% Filter the critical points for t > 0
positiveCritPointsX = sym([]);
for i = 1:length(critPointsX)
if isAlways(imag(critPointsX(i)) == 0) && isAlways(critPointsX(i) >= 0)
positiveCritPointsX(end+1) = critPointsX(i);
end
end
% Evaluate velocity at critical points and endpoints
displacmentValues = double(subs(mov, t, [positiveCritPointsX; Inf]));
displacmentValues(end+1) = subs(mov, t, 0);
maxDisplacment = abs(max(abs(displacmentValues)));
% disp(maxDisplacment); % Print the numeric value of the maximum displacment
%% For finiding max acceleration:
% Find the critical points of velocity
critPointsA = solve(diff(acc, t), t);
% Filter the critical points for t > 0
positiveCritPointsA = sym([]);
for i = 1:length(critPointsA)
if isAlways(imag(critPointsA(i)) == 0) && isAlways(critPointsA(i) >= 0)
positiveCritPointsA(end+1) = critPointsA(i);
end
end
% Evaluate acceleration at critical points and endpoints
accValues = double(subs(acc, t, [positiveCritPointsA; Inf]));
accValues(end+1) = subs(acc, t, 0);
maxAcceleration = abs(max(abs(accValues)));
end
Thank you!

采纳的回答

Torsten
Torsten 2023-5-30
移动:Torsten 2023-5-30
Try a solution with c, k, m, x0 and v0 being symbolic variables.
Then you only need to "subs" the numerical values for the parameters into the expression "critPointsX" and continue in your evaluation.
  2 个评论
Aharon Renick
Aharon Renick 2023-5-30
I changed the papametrs in the input to be:
function [maxDisplacment,maxAcceleration] = SystemTest(c_num,k_num,m_num,Tmax,x0,v0)
I assume i define the variables as symbolic like this:
syms x(t) c m k
But how do I "subs" the numerical values for the parameters into the expression "critPointsX"?
Torsten
Torsten 2023-5-30
%% Analytic solving:
syms x(t)
syms m c k x0 v0 real
eq = m*diff(x,t,t) + c*diff(x,t) + k*x == 0;
cond = [x(0) == x0, subs(diff(x(t), t), t, 0) == v0];
mov = dsolve(eq, cond);
vel = diff(mov,t);
acc = diff(mov,t,t);
%% For finiding max displacment:
% Find the critical points of velocity
critPointX = solve(diff(mov, t), t)
critPointX = 
mnum = 1;
cnum = 1;
knum = 1;
x0num = 0;
v0num = 1;
critPointXnum = double(subs(critPointX,[m c k x0 v0],[mnum,cnum,knum,x0num,v0num]))
critPointXnum = 1.2092

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by