I'm not sure what I'm doing wrong that gives me a error

조회 수: 3 (최근 30일)
Umut Ayyildiz
Umut Ayyildiz 2019년 2월 4일
댓글: Rik 2019년 2월 5일
The question is:
The maximum height h achieved by an object thrown with a speed v at an angle θ to the horizontal, neglecting drag, is
h =(v^2*(sin(θ))^2)/(2g)
Create a table showing the maximum height for the following values of v and θ:
v = {10,12,14,16,18,20}m/s, θ = {50,60,70,80}
The rows in the table should correspond to the speed values, and the columns should correspond to the angles.I have this so far
v=[10, 12, 14, 16, 18, 20]
t=[50, 60, 70, 80]
g=9.8
h=((v^2).*(sind(t)^2))/(2g)
After I run it, I get this error:
Invalid expression. Check for missing
multiplication operator, missing or
unbalanced delimiters, or other syntax
error. To construct matrices, use brackets
instead of parentheses.

답변 (3개)

Rik
Rik 2019년 2월 4일
편집: Rik 2019년 2월 5일
You should transpose your speed vector, so it has one value per row. If you then change your expression to the one below, the implicit expansion will make sure the end result is a matrix.
v=[10, 12, 14, 16, 18, 20]';
t=[50, 60, 70, 80];
g=9.8;
h=((v.^2).*(sind(t).^2))./(2*g)
  댓글 수: 2
Umut Ayyildiz
Umut Ayyildiz 2019년 2월 5일
This code still gives me the same error. I instead changed the code to
v=[10, 12, 14, 16, 18, 20]
t=[50; 60; 70; 80]
g=9.8
h=(v.^2.*(sind(t).^2))./(2*g)
I just added semicolons, I'm not sure what they do but I got an output.
Rik
Rik 2019년 2월 5일
If you copy my code exactly, it will return an output.
What your code will do is create a row vector v and a column vector t, resulting in a 4x6 matrix h, which means that every row corresponds to a value of theta and every column corresponds to a value of v, which is the opposite of your assignment.
There are two delimiters in Matlab for making arrays, the comma and the semicolon. The comma separates columns, and the semicolon starts a new row. You can either input a vector as a column vector, or input it as a row vector and then transpose it (like I did with v).

댓글을 달려면 로그인하십시오.


Kevin Phung
Kevin Phung 2019년 2월 4일
your time vector and velocities are not the same length.. I'm guessing you're incrementing by 10.
you're also missing some element wise operations (indicated with a period) and youre missing a multiplication for 2*g.
so:
v=[10, 12, 14, 16, 18, 20]
t=[50, 60, 70, 80, 90, 100]
g=9.8
h=((v.^2).*(sind(t).^2))/(2*g)
should work and give you an array for height

Steven Lord
Steven Lord 2019년 2월 4일
If you open this code in the MATLAB Editor, you should see that there is a red square in the upper-right corner of the Editor window. That means Code Analyzer has detected an error in your code that will prevent it from running. Below that red square is a red line corresponding to line 4 of the code. If you look closely at line 4, you should see that the g near the end of the line is underlined in red. That's where the error occurs.
If you're multipying two variables together or a number and a variable, you must include the multiplication sign. When you add the * between 2 and g on that line the red underline goes away and the red square turns orange.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by