Solving equations with parameters and then inputting different values

12 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

Torsten
Torsten el 30 de Mayo de 2023
Movida: Torsten el 30 de Mayo de 2023
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 comentarios
Aharon Renick
Aharon Renick el 30 de Mayo de 2023
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 el 30 de Mayo de 2023
%% 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

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by