MATLAB logical functions and selection structures

In summary: 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 MAX function.
  • #1
cookiemnstr510510
162
14
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:
Physics news on Phys.org
  • #2
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
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
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
cookiemnstr510510 said:
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
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
FactChecker said:
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
RPinPA said:
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
FactChecker said:
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
jedishrfu said:
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
cookiemnstr510510 said:
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
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
sysprog said:
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

1. What are logical functions in MATLAB?

Logical functions in MATLAB are built-in functions that allow you to perform logical operations, such as AND, OR, and NOT, on arrays or matrices. These functions return a logical output of either true or false.

2. How do I use logical functions in my code?

To use logical functions in your code, you first need to define or create the arrays or matrices that you want to perform the logical operations on. Then, you can use the logical functions, such as logical AND (&&), logical OR (||), and logical NOT (~), to compare the elements of the arrays or matrices and get a logical output.

3. What are selection structures in MATLAB?

Selection structures in MATLAB are programming constructs that allow you to execute different blocks of code based on a specific condition. There are two types of selection structures: if statements and switch statements.

4. How do I use if statements in MATLAB?

To use if statements in MATLAB, you first need to define the condition that you want to check. Then, you can use the if keyword, followed by the condition in parentheses, and the code that you want to execute if the condition is true inside curly braces. You can also add an optional else statement to execute a different block of code if the condition is false.

5. What is the difference between if and switch statements in MATLAB?

The main difference between if and switch statements in MATLAB is that if statements allow you to check for multiple conditions, while switch statements only allow you to check for one condition with multiple possible values. Additionally, if statements can be nested within each other, while switch statements cannot.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
829
  • Engineering and Comp Sci Homework Help
Replies
1
Views
943
  • Engineering and Comp Sci Homework Help
Replies
3
Views
815
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
Back
Top