How Can I Resolve Loop Stuck Issues in MATLAB's Shooting Method?

In summary, the conversation discusses using the shooting method to determine the angle of a projectile and encountering issues where the angle gets stuck between two values. The speaker is considering using a try ... catch statement but is unsure of how to implement it. They are also seeking advice on how to handle the situation where the projectile is not getting any closer to its target.
  • #1
CaspianTiger
17
0
Hi,

I am trying to use the shooting method to determine the angle of a projectile. However for some values of distance and initial velocity my value for angles get stuck between two values.
I want to put a piece of code into my shooting method which can either detect that my while loop has become stuck between two values or after a certain number of values offers an error message suggesting a different initial velocity for that distance.

I was thinking of using a try catch statement however i am not sure how these work and i am not sure how to state i want my error message to occur after a certain value of iterations of my while loop.

If you want my present code i can post it.


Any help would be great.

Thanks
 
Physics news on Phys.org
  • #2
A try ... catch block won't be of much use to you since these are used to trap errors such as division by zero and others.

Are the angles you get stuck on equidistant from 45 degrees? I.e., is one 45 degrees + alpha and the other one 45 degrees - alpha? For a given muzzle velocity, the maximum range comes when the projectile is fired at and angle of 45 degrees from the horizontal. All other angles produce a shorter distance, which means that for a given target distance that is less than the maximum range, there will be two angles that will place the projectile at that distance. I think that's what you're coming up against.

One way around this is to calculate how close the projectile is to its target. If you next angle value doesn't get you any closer to the target, exit the loop.
 
  • #3



Hi there,

I understand your problem and can suggest a possible solution. Instead of using a try catch statement, you can implement a counter within your while loop that keeps track of the number of iterations. Once the counter reaches a certain value, you can add an if statement to your code that checks if the angle is stuck between two values. If it is, you can print an error message and suggest trying a different initial velocity. This way, you can control the number of iterations and prevent the loop from getting stuck.

Also, it would be helpful if you could provide your current code so I can better understand the problem and suggest specific changes.

Best of luck with your project!
 

FAQ: How Can I Resolve Loop Stuck Issues in MATLAB's Shooting Method?

What is the purpose of the code in "A simple problem with matlab code"?

The purpose of the code is to solve a specific problem using the programming language MATLAB. It may involve manipulating data, performing calculations, or creating visualizations.

What is the structure of the code in "A simple problem with matlab code"?

The code is typically structured into blocks, with each block containing a specific set of instructions or commands. The code may also contain comments to explain what each block is doing.

What are some common errors that may occur in "A simple problem with matlab code"?

Some common errors include syntax errors, such as missing or misplaced parentheses or semicolons, as well as logic errors, where the code does not produce the desired output due to incorrect commands or calculations.

How can I troubleshoot and fix errors in "A simple problem with matlab code"?

To troubleshoot errors, it is helpful to carefully read any error messages generated by MATLAB and check for any obvious mistakes in the code. Using debugging tools and testing small sections of code can also help identify and fix errors.

How can I improve the efficiency and functionality of "A simple problem with matlab code"?

To improve efficiency, it is important to optimize the code by using built-in functions and avoiding unnecessary calculations or loops. To enhance functionality, you can also add additional code to handle different scenarios or expand the capabilities of the code.

Similar threads

Replies
3
Views
1K
Replies
3
Views
1K
Replies
12
Views
3K
Replies
2
Views
2K
Replies
4
Views
1K
Replies
4
Views
3K
Back
Top