0
我真的停留在這段代碼上。我試圖模擬一個物理問題來想象一個問題。它有2個掛鉤和兩個彈簧連接到x方向有50N橫風的質量塊。我不太清楚如何在x和y中合併限制。請任何幫助MatLab中的物理模擬
%position of peg1
pegX1 = 3;
pegY1 = 10;
%position of peg2
pegX2 = 8;
pegY2 = 10;
%position of mass
xPos = 5;
yPos = 6;
%velocity of mass
xVel = 0;
yVel = 0;
%mass of mass
m = 2;
%constant of springs
k1 = 20;
k2 = 25;
%equilibrium distance of spring
eq1 = 2;
eq2 = 1.5;
%constant of gravity
g = 5;
%define time domain
dt = 0.1;
t = 0:dt:100;
%record all previous positions for plot
positions = zeros(2,length(t));
for i = 1:length(t)
%find angle between peg1 and mass
theta1 = direction(xPos,yPos,pegX1,pegY1);
%find angle between peg2 and mass
theta2 = direction(xPos,yPos,pegX2,pegY2);
%calc X resultant force
FresX = -1*(eq1-dist(xPos,yPos,pegX1,pegY1,pegX2,pegY2))*k1*k2*cos(theta1+theta2);
%calc Y resultant force
FresY = -1*(eq1-dist(xPos,yPos,pegX1,pegY1,pegX2,pegY2))*k1*k2*sin(theta1+theta2) - m*g;
%calculate acceleration due to force
aX = FresX/m;
aY = FresY/m;
%change velocity based on acceleration
xVel = xVel + aX*dt;
yVel = yVel + aY*dt;
%change position based on velocity
xPos = xPos + xVel*dt;
yPos = yPos + yVel*dt;
%record position for plot
positions(1,i) = xPos;
positions(2,i) = yPos;
%plot position
p1 = plot(positions(1,1:i),positions(2,1:i),'r');
axis([0 4.5 0 4.5]);
grid on
pause(0.1)
end