"Matrix Dimensions Must Agree" Debug
이전 댓글 표시
I am writing a code for the motion of a projectile, where I want the results to plot 5 graphs. My issue lies within the y(n+1) part of the code. I have tried both using and not using the "." with my multiplication and division, to no avail. Thank you in advance for the help!
NOTE: My plot should stop once the 'y' value is less than 0, because this is when the projectile hits the ground.
clear; clc;
g=9.81; %m/s/s
vo=100; %m/s
theta=[15:15:85]; %degrees
yo=1; %m
xo=0; %m
y(1)=1; %initial position y in meters
x(1)=0; %initial position x in meters
t(1)=0;
dt=.1;
n=1;
while y(n) >0
t(n+1)=n+dt;
y(n+1)=yo+(vo.*sin(theta).*t)-((g./2).*(t^2));
x(n+1)=xo+(vo.*cos(theta).*t);
n=n+1;
end
plot(x,y)
댓글 수: 5
The code need lots of changes......your theta is avector...and the way, you have used t, takes it also as a vector. As the loop grows, dimensions of theta and t will be different, and it pops up error.
1. You need to rethink on your code
2. You need to read about pre-allocation
3. theta is in degrres, so you have to use sind/ cosd
4. Think about t, it should be vector or a scalar? It should be a scalar.
5. The way, you save the output, should be changed. Coz the out put is a vector.
6. The way while loop used, also should be changed.
Bailey Smith
2018년 5월 31일
This is exactly the way my prof said it should be set up. The value for t should be changing until t hits 0.
Walter Roberson
2018년 5월 31일
You start t at 0 and you add 0.1 each time. The only point at which that is going to reach 0 is at the very beginning.
Bailey Smith
2018년 5월 31일
I meant when y reaches 0. My bad. I want to stop plotting when the projectile hits the ground.
Walter Roberson
2018년 5월 31일
You are tracking multiple objects, one for each angle in theta. You need to decide whether you want to stop when the first one hits the ground, or when they have all hit the ground. If it is when they have all hit the ground, you have to decide what you want to do with the ones that went below the ground while the others were still falling.
답변 (1개)
Walter Roberson
2018년 5월 31일
t(n+1)=n+dt;
so t is getting larger as you go.
t starts at as scalar before the loop, and in the first iteration of the loop expands to a vector of length 2
y(n+1)=yo+(vo.*sin(theta).*t)-((g./2).*(t^2));
theta is a vector of length 5, and you have sin(theta).t . But t is a vector of length 2, and you cannot .* between a 1 x 5 and a 1 x 2.
Then in the right hand part of the expression, you have t^2 but t is a vector and you cannot ^2 a vector. You can ^2 a square matrix, or you can .^2 a vector.
yo+(vo.*sin(theta).*t(n+1))-((g./2).*(t(n+1)^2))
would get rid of the problems about t being a vector of a different size than theta. However, you would still have the difficulty that theta is a vector of length 5, so you are going to be calculating 5 different results and trying to store them in the single location y(n+1)
clearvars
g=9.81; %m/s/s
vo=100; %m/s
theta=[15:15:85]; %degrees
yo=1; %m
xo=0; %m
y(1, :) = 1 * ones(size(theta)); %initial position y in meters
x(1, :) = 0 * ones(size(theta)); %initial position x in meters
t(1)=0;
dt=.1;
n=1;
while y(n) >0
t(n+1)=n+dt;
y(n+1,:)=yo+(vo.*sin(theta).*t(n+1))-((g./2).*(t(n+1)^2));
x(n+1,:)=xo+(vo.*cos(theta).*t(n+1));
n=n+1;
end
plot(x,y)
댓글 수: 1
Walter Roberson
2018년 5월 31일
while y(n) >0
should be
while any(y(n,:)>0)
카테고리
도움말 센터 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!