Solving equations with parameters and then inputting different values
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aharon Renick
el 30 de Mayo de 2023
Comentada: Torsten
el 30 de Mayo de 2023
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 comentarios
Respuesta aceptada
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
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)
mnum = 1;
cnum = 1;
knum = 1;
x0num = 0;
v0num = 1;
critPointXnum = double(subs(critPointX,[m c k x0 v0],[mnum,cnum,knum,x0num,v0num]))
Más respuestas (0)
Ver también
Categorías
Más información sobre Calculus en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!