MATLAB logical functions and selection structures

  • #1
Moved from a technical forum, so homework template missing
Hello All,
I have a question regarding a MATLAB homework problem. I am learning about logical functions and selection structures.
Here is the question:
The height of a rocket (in meters) can be represented by the following equation:
height=(2.13*t^2)-(0.0013*t^4)+((0.000034*t^(4.751))
create a vector of time(t) values from 0 to 100 at 2 sec intervals
A)Use the find function to determine when the rocket hits the ground to within two seconds.
B) use the max function to determine the maximum height of the rocket and the corresponding time.

I believe I can do part A, part B is what I am struggling with. Here are my attempts:

A)
Matlab:
t=0:2:100
height=(2.13*(t.^2))-(0.0013*(t.^2))+(0.000034*(t.^4.751))
posHeight=find(height>0)
M=max(posHeight)
ans_a=t(a)
<Moderator's note: code tags added. Please use them.>

My attempt at B:
Matlab:
MaxHeight=max(height)
CorrTime=t(MaxHeight)
It is at this part that I get an error saying: Array indices must be positive integers or logical values...
Any help would be appreciated. I assume it has something to do with indexing?

Thanks
 
Last edited by a moderator:

Answers and Replies

  • #2
12,088
5,761
Why don't you look at your intermediate variables via a matlab print command or via the debugger and see if you can work it out first?

There should be a panel that shows the values of your variables in your workspace.
 
  • Like
Likes cookiemnstr510510
  • #3
1,649
905
It seems that in your code, t is being interpreted as an array (of one dimension), so t(n) should be the nth element of the array. Yes, that has something to do with indexing, and it's not merely your assumption; the message told you that when it mentioned 'indices'. Perhaps you could include all of the relevant code, preferably inside [ code=matlab] 'your code' [ /code] tags (without the space after the opening bracket).
 
  • Like
Likes cookiemnstr510510
  • #4
RPinPA
Science Advisor
Homework Helper
571
319
Your height formula does not match what you were given. But I second the suggestion to print the intermediate variables. You can use the disp() function or just set a breakpoint. If you haven't used the Matlab debugger, you should try it out and get used to it. Debuggers are extremely valuable in any language.

What is the value of MaxHeight? Stop your program there and check that.

Edit: Actually, if this is a script, all of these variables will be defined in your workspace. So you can just check the value of MaxHeight after it runs.
 
Last edited:
  • Like
Likes cookiemnstr510510
  • #5
34,151
5,766
The height of a rocket (in meters) can be represented by the following equation:
height=(2.13*t^2)-(0.0013*t^4)+((0.000034*t^(4.751))
create a vector of time(t) values from 0 to 100 at 2 sec intervals
A)Use the find function to determine when the rocket hits the ground to within two seconds.
<snip>

I believe I can do part A, part B is what I am struggling with. Here are my attempts:

A) t=0:2:100
height=(2.13*(t.^2))-(0.0013*(t.^2))+(0.000034*(t.^4.751))
posHeight=find(height>0)
M=max(posHeight)
ans_a=t(a)
1. As already noted, your formula in the matlab code isn't the same as the formula you're supposed to use.
2. The last line of your code above uses a variable, a, that hasn't been initialized. Since t is a vector of times, possibly that line of code is what is generating the error -- array indexes have to be nonnegative integers.
 
  • Like
Likes cookiemnstr510510
  • #6
FactChecker
Science Advisor
Gold Member
5,786
2,152
MaxHeight is not an integer. It is the actual maximum floating point value of height. If you want the indices with the maximum values, use [MaxHeight, MaxIndices] = ...
 
Last edited:
  • Like
Likes cookiemnstr510510
  • #7
RPinPA
Science Advisor
Homework Helper
571
319
MaxHeight is not an integer. It is the actual maximum floating point value of height. If you want the indices with the maximum values, use [MaxHeight, MaxIndices] = ...
Exactly, but I was hoping OP would do the debugging and have an aha moment when seeing the value of MaxHeight.
 
  • Like
Likes cookiemnstr510510 and FactChecker
  • #8
FactChecker
Science Advisor
Gold Member
5,786
2,152
Exactly, but I was hoping OP would do the debugging and have an aha moment when seeing the value of MaxHeight.
Sorry. I probably should have stayed in my lane. From the looks at the code, I wasn't sure that the OP would know how to get the indices from the max function. I think that syntax is unusual in programming languages.
 
  • Like
Likes cookiemnstr510510
  • #9
RPinPA
Science Advisor
Homework Helper
571
319
Sorry. I probably should have stayed in my lane. From the looks at the code, I wasn't sure that the OP would know how to get the indices from the max function. I think that syntax is unusual in programming languages.
No, it's fine, and I doubt OP would have stumbled on the two-output version of MAX which is needed here, without somebody suggesting it. I was about one post away from saying what you said anyway, but I do really believe in experimenting with Matlab, typing different things at the command line to explore what is happening, and so I'm STILL encouraging OP to do that. Enter your code one line at a time at the command prompt. Check out the values of variables.

And about the two-output MAX: Read HELP files carefully, there is a lot of power hidden in the various optional versions of the built-in functions.
 
  • Like
Likes cookiemnstr510510
  • #10
Why don't you look at your intermediate variables via a matlab print command or via the debugger and see if you can work it out first?

There should be a panel that shows the values of your variables in your workspace.
define intermediate variables. Do you mean my entire code leading up to the final answer? And I assume you mean look in the Workspace as to what their defined values are. What I know about the debugger is you can hover your mouse over the red bars on the right hand side of say a script file and read the error messages.
 
  • #11
RPinPA
Science Advisor
Homework Helper
571
319
define intermediate variables.
When an expression doesn't give you the desired result, check out everything that went into that expression. Build up the expression bit by bit. If an input to the expression isn't what you expect, try reproducing the calculation of that input.
 
  • Like
Likes cookiemnstr510510
  • #12
Okay, after looking at this with a fresh set of eyes (good nights sleep) I find this:
MATLAB:
[maxHeight,locOfMax]=max(height) %This finds the the actual number the max height is 1.4695*10^3 and the location within the array, the 21st element.
and_b=t(locOfMax) %This index's (calls the value of time) that corresponds to the 21st element of the time array(40 seconds)
To address what I was doing before (in original post):
I was finding the max height which was giving me a value in the height array. I was then trying to index the time array using that value, however that didn't make sense because the time array didn't have 1.4695*10^3 values, so when trying to call the 1.4695*10^3 location MATLAB was telling me that there was only 51 values. This is why I was getting this error.
Is this the right logic as to why my previous method was incorrect?

Sorry if my terminology for some things is incorrect, I am still learning MATLAB's language and am not 100% with all the terms.
 
  • #13
1,649
905
The error was not for an out of bounds subscript condition; it was for a non-integer subscript condition.

You're not using logical values to index into a vector, so the message "Array indices must be positive integers or logical values." refers to the requirement that the index value must be an integer, and your number is not an integer.

Your vector declaration (t=0:2:100) specifies a 1-dimensional array of 51 even-numbered elements starting from 0 and ending with 100 (inclusive).

If you set a variable called 'whichone' equal to 3.5, or any other non-integer value, and then reference t(whichone), Matlab will tell you that the index, in this case whatever value you've assigned to 'whichone', must be a positive integer, because 3.5, a decimal fractional number, is not the right kind of value for it to be able to decide which of the 51 values is being referenced.

If you set 'whichone' equal to 1470, and referenced t(whichone), you would get a different error message, one that said subscript out of bounds.
 
Last edited:
  • Like
Likes cookiemnstr510510 and FactChecker
  • #14
The error was not for an out of bounds subscript condition; it was for a non-integer subscript condition.

You're not using logical values to index into a vector, so the message "Array indices must be positive integers or logical values." refers to the requirement that the index value must be an integer, and your number is not an integer.

Your vector declaration (t=0:2:100) specifies a 1-dimensional array of 51 even-numbered elements starting from 0 and ending with 100 (inclusive).

If you set a variable called 'whichone' equal to 3.5, or any other non-integer value, and then reference t(whichone), Matlab will tell you that the index, in this case whatever value you've assigned to 'whichone', must be a positive integer, because 3.5, a decimal fractional number, is not the right kind of value for it to be able to decide which of the 51 values is being referenced.

If you set 'whichone' equal to 1470, and referenced t(whichone), you would get a different error message, one that said subscript out of bounds.
Wow, thank you for the detailed explanation. This is what I need since I am not too advanced yet.
I appreciate your time and effort! It makes more sense now.
 
  • Like
Likes sysprog

Related Threads on MATLAB logical functions and selection structures

  • Last Post
Replies
4
Views
2K
Replies
1
Views
2K
  • Last Post
Replies
6
Views
2K
Replies
2
Views
6K
  • Last Post
Replies
2
Views
1K
Replies
7
Views
2K
  • Last Post
Replies
1
Views
1K
Replies
3
Views
2K
Top