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!
댓글 수: 0
채택된 답변
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
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)
mnum = 1;
cnum = 1;
knum = 1;
x0num = 0;
v0num = 1;
critPointXnum = double(subs(critPointX,[m c k x0 v0],[mnum,cnum,knum,x0num,v0num]))
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!