Dynamics Problem using two moving object

I've been having problems solving this equation in MATLAB. Solving on paper is more intuitive to me and I'm struggling in making the language jump. So far, here is my code:
pA = [0 14]; %position of ship A at start
pB = [-25 0]; %position of ship B at start
vA = [0 -7]; %velocity of ship A at start
vB = [16 0]; %velocity of ship B at start
for t=1:4;
dA=(pA+vA*t) %position of A based on time
dB=(pB+vB*t) %position of B based on time
end
I can't seems to figure out how to get the positions of each of the ships in relation to each other. Once I do that, I was attempting to use the zeros function to solve for the 8 mile visibility. couldn't get it to work. Thanks for your help!

 採用された回答

Birdman
Birdman 2017 年 10 月 27 日
編集済み: Birdman 2017 年 10 月 27 日

0 投票

You can find position of A with repsect to B by the following:
pAwrtB=dA-dB;%position of A with repsect to B
vice versa:
pBwrtA=dB-dA;%position of B with repsect to A
Because imagine every time we spot our position on the x-y coordinate axis, we actually reference to the origin and to this
dOurPosition - [0 0]
But in this situation, relative positions are found just as above. Change the for loop like this:
for t=1:4
dA=pA+vA*t;
dB=pB+vB*t;
pBwrtA=dB-dA;%position of B with repsect to A
pAwrtB=dA-dB;%position of A with repsect to B
end

4 件のコメント

BlkHoleSun
BlkHoleSun 2017 年 10 月 27 日
Thank you for the help. I've taken what you've taught me and continued to plug away at it. I have one remaining question. I can't figure out how to make my y-axis span the time from 07:00 to 11:00 in one hour increments. Below is my code and you'll see that I'm very close to figuring it out. I believe the problem is in the way I've defined 't'.
%Defining the variables
pA = [0 14]; pB = [-25 0];
vA = [0 -7]; vB = [16 0];
t=linspace(0,4,100);
dT=zeros(size(t));
%compute a loop for the distance
for i=1:length(t)
dA=pA+vA*t(i);
dB=pB+vB*t(i);
dT(i)=sqrt(dA(2).^2+dB(1).^2);
end
%Identify the row and column for when the distance is less than 8
[r, c]=find(dT<=8);
x1=t(min(c)); x2=t(max(c));
%plot distance curve and visibility line
hold on
plot(t, dT)
plot(t,8*ones(size(t)))
%label graph and add text string
xlabel('\bf\itTime, hours+7am \rm');
ylabel('\bf\itDistance, miles\rm');
z=text(.1,40,['The ships will see each other between the times of ' num2str(x1), 'and ', num2str(x2)'.']);
datetick('x', 'HH:MM');
BlkHoleSun
BlkHoleSun 2017 年 10 月 27 日
Correction: I can't figure out how to make my x-axis span the time from 07:00 to 11:00 in one hour increments.
Birdman
Birdman 2017 年 10 月 30 日
Use the m file. I used xticklabels to do it.
BlkHoleSun
BlkHoleSun 2017 年 10 月 30 日
Thanks for the help. I've learned a lot about graphing this problem!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

タグ

質問済み:

2017 年 10 月 27 日

コメント済み:

2017 年 10 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by