Solving equations with parameters and then inputting different values
18 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!
0 commentaires
Réponse acceptée
Torsten
le 30 Mai 2023
Déplacé(e) : Torsten
le 30 Mai 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 commentaires
Torsten
le 30 Mai 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)
mnum = 1;
cnum = 1;
knum = 1;
x0num = 0;
v0num = 1;
critPointXnum = double(subs(critPointX,[m c k x0 v0],[mnum,cnum,knum,x0num,v0num]))
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!