Why does my script keep getting the Error using ./ Matrix dimensions must agree when calculating y in the script

my function
function [d,t,y_max] = fire_projectile(v,y0,theta)
%v is initial velocity
%y0 is launch height
%theta is launch angle
g=9.81;
d = ((v.*cos(theta))/g).*((v.*sin(theta))+((((v.*sin(theta)).^2)+(2.*g.*y0))).^0.5);
%d is the overall distance traveled
y_max = y0 + ((v.*sin(theta))/(2.*g));
%y_max is max height reached
t= d/(v.*cos(theta));
%time travelled
my script
clear
g=9.81; %value for gravity
v=input('input initial velocity ','s');
y0 = input('input launch height ','s');
theta=10;
[d,t,y_max] = fire_projectile(v,y0,theta);
x=0:1:d(1);
y = (x*tan(theta))-((g*(x.^2))./(2.*((v.*cos(theta)).^2)));
plot(y,x);
%y is the height at position x
i only get the error when inputs for v and y0 are greater than 10, which they need to be.
edit:
these are the 2 errors i get
Error using ./
Matrix dimensions must agree.
Error in fire_projectile_script (line 8)
y = (x*tan(theta))-((g*(x.^2))./(2.*((v.*cos(theta)).^2)));
outputs seen in workspace
d: [-60.5,-58.2]
g: 9.81
t: 1.43
y_max: [47.6, 46.7]

8 个评论

after running the code type d in command window and see the mistake you made , why do you people always supress the output?
i dont supress the output, i have workspace open so see all outputs there
done, not sure what its supposed to achieve as the command window is seperate to my script
ok so i changed it, know I get the error that matrix dimensions must agree. also still get this
Error in fire_projectile_script (line 8)
y = (x.*tan(theta))-((g.*(x.^2))./(2.*((v.*cos(theta)).^2)))
can you organise your code and provide all the necessary datas(v,y0,theta) to test your code?
new script code
clear
g=9.81; %value for gravity
v=30
y0=10
theta=10;
[d,t,y_max] = fire_projectile(v,y0,theta);
x=0:1:65;
y = (x.*tan(theta))-((g.*(x.^2))./(2.*((v.*cos(theta)).^2)));
plot(y,x);
%y is the height at position x

请先登录,再进行评论。

 采纳的回答

clear
g=9.81; %value for gravity
v=30
y0=10
theta=10;
[d,t,y_max] = fire_projectile(v,y0,theta);
x=0:1:65;
y = (x.*tan(theta))-((g.*(x.^2))./(2.*((v.*cos(theta)).^2)));
plot(y,x);
function [d,t,y_max] = fire_projectile(v,y0,theta)
%v is initial velocity
%y0 is launch height
%theta is launch angle
g=9.81;
d = ((v.*cos(theta))/g).*((v.*sin(theta))+((((v.*sin(theta)).^2)+(2.*g.*y0))).^0.5);
%d is the overall distance traveled
y_max = y0 + ((v.*sin(theta))/(2.*g));
%y_max is max height reached
t= d/(v.*cos(theta));
end

11 个评论

thank you for your help, also looking at the code you wouldnt be able to help me work out why d is coming out negative when all the inputs are positive
cos(theta) %this is why
so if theta is degree then you should simply put
cosd(theta)
if theta is degrees then f you want to convert it to radians then
cos(deg2rad(theta))
but for the theta value of 10, cos(theta) is positive, so should not make d negative?
i had 10 degrees, so just converted everything to radians, thank you for the comment
This is my final script
clear
g=9.81; %value for gravity
vi=input('input initial velocity ','s');
yi=input('input launch height ','s');
v=vi;
y0=yi;
%theta is the angle of launch
%y is the height at position x
specifically focusing on the beginning. when the inputs v and y0 are numbers the script works fine, however when im asking for an imput i get this error occuring.
Error using +
Matrix dimensions must agree.
Error in fire_projectile_script (line 12)
y1 = y0+(x1.*tan(deg2rad(theta)))-((g.*(x1.^2))./(2.*((v.*cos(deg2rad(theta))).^2)));
i dont know why I am getting this when the inputs are the same, I am just choosing an input.
edit; i removed half the code so that my entire answer cannot be seen as the question only relates to the first part
vi=input('input initial velocity ','s');
yi=input('input launch height ','s');
should be
vi=input('input initial velocity ');
yi=input('input launch height ');

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by