Matlab: Finding the right angle

In summary, your code calculates the position of the rock using trigonometric functions, and uses the bisection method to find the angle a which will make it land 20 meters away from the shooter.
  • #1
Adel A
5
0
Hi!

I have a question about an assignment.
It is about throwing a rock which should land 20 meters away from the "shooter". My job is to find the angle "a" which makes the rock land 20 meters away.

The motion is given by a multivariable, second order differential equation which I have already "converted" into a first order differential equation. I am only allowed to use the Runge-Kutta-method, and not allowed to use ODE45.

I have managed to write a code which works fine, but I need help with making a loop of some sort, in order to find a precise angle, a, which makes the rock land 20 meters away from the shooter.
I know that the angle should be approximately 1 radian.

Here is the code:

Code:
clear all
close all
clc

format long

%g = 9.81;
a = pi/4;
a = 1;

x0 = [0;1.5];
xPrim0 = 19.*[cos(a);sin(a)];

K = [0.02 0; 0 0.065];
f = [0; -9.81];

x = [];                         % Vi ska räkna ut X (positionen), men vad skickar vi till funktionen?
xPrim = 19.*[cos(a);sin(a)];    % Ska det multipliceras med 19?
                                % xPrim är väl inte en funktion av vinkeln
                                % efter kastet?
h = 0.3;
T = 100;
hojd = 1.5;

%Q = [0.1 1 1.5 3.1];

%c0 = [0.1;0];
u = [x0; xPrim0];
u0 = [x0;xPrim0];

%F = [xPrim; f - norm(xPrim).*K*xPrim];

W = [];

% for i = 1:4

% u0 = [u(i);0];

u = u0;
U = u;

tt = 0:9;

for t=tt(1:end-1)

    k1 = Fproj(u);
    k2 = Fproj(u+0.5*h*k1);
    k3 = Fproj((u+0.5*h*k2));
    k4 = Fproj((u+k3*h));

    u = u + (1/6)*(k1+2*k2+2*k3+k4)*h;

    U = [U u];

end

% W = [W U];

plot(U(1,1:end),U(2,1:end))
hold on
line([0 25], [0 0])
line([20 20], [-5 5])
 
Physics news on Phys.org
  • #2
I suggest that you encapsulate some of that code in a function that, given an angle a, calculates where the rock lands. It would be also easier if it returned the number as the distance to the desired value, such that it returns 0 when it is on target.

What you need then is to use some method for finding roots. You could use the Newton-Raphson method, but it is probably overkill for such a simple 1D problem. The simplest you could try is the bisection method.
 
Last edited:
  • Like
Likes FactChecker and Adel A

1. How do I find the right angle in Matlab?

To find the right angle in Matlab, you can use the atan2 function which calculates the inverse tangent of the ratio of two sides of a right triangle. This function takes in the values of the adjacent and opposite sides and returns the angle in radians.

2. Can I convert the angle from radians to degrees in Matlab?

Yes, you can use the rad2deg function to convert the angle from radians to degrees in Matlab. This function takes in the angle in radians as input and returns the corresponding angle in degrees.

3. How can I find the angle between two vectors in Matlab?

To find the angle between two vectors in Matlab, you can use the dot product and the norm function. First, calculate the dot product of the two vectors and then divide it by the product of their norms. Finally, use the acos function to find the inverse cosine of this value, which will give you the angle in radians.

4. Is there a built-in function to find the angle of a complex number in Matlab?

Yes, you can use the angle function in Matlab to find the angle of a complex number. This function takes in the complex number as input and returns the angle in radians.

5. How can I plot the angle of a function in Matlab?

To plot the angle of a function in Matlab, you can use the plot function and specify the angle as the y-axis and the input values as the x-axis. This will create a graph of the function's angle over the specified range of input values.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
15
Views
4K
Back
Top