Anson
Anson

Reputation: 11

Matlab Code about trajectory

I am making a code about projectile with friction. I think that I made some mistakes but I can't find it out. It is a MATLAB function called targetangle.m that calculates the optimal targeting angle θ for the weapon given the launching speed vs , target distance d and desired accuracy e .


 function[ theta dif ] = targetangle(vs,d,e)

     % Input the initial condition:

     th = 1:0.1:45;

     for theta=th 

         k = 0.2;

         g = 9.81;

         dt = 0.001;

         the = theta * pi/180.;

         u(1) = vs * cos(the);

         v(1) = vs * sin(the);

         % Launch pad location:

         x(1) = 0.;

         y(1) = 0.;

         for t=1:1:6000;

             u(t+1) = u(t)- dt * (k * sqrt(u(t)^2+v(t)^2) * u(t));

             v(t+1) = v(t)- dt * (k * sqrt(u(t)^2+v(t)^2) * v(t) + g);

             x(t+1) = x(t) + u(t) * dt;

             y(t+1) = y(t) + v(t) * dt;

             % Determination of when the object hits ground:

             if y(t+1) < 0

                 dif = min(abs(x(t+1) - d));

                 plot(x,y)

             end

             if y(t+1) < 0;

                 break

             end

         end

         % Once object hits terminate the computations with a break:

         if y(t+1) < 0; 

             break

         end

     end

     if dif < e

         disp(dif)

     else

         theta = NaN

     end

when you input like this:

vs = 100;

d = 12;

e = 1;

[ theta dif ] = targetangle(vs,d,e)

The output should be:

theta =

    4

dif =

    0.0323

But my code output is :

theta =

    NaN


theta =

    NaN


dif =

    3.6718

Upvotes: 0

Views: 1207

Answers (1)

Jannik Michel
Jannik Michel

Reputation: 366

The problem with your calculations is, that you do not save the optimal result during your for-loop. So your result is the (very correct) result of your last loop cycle.

For theta = 45 deg you get theta=NaN because you say so in the last if clause. (btw: the missing semicolon is the reason you get theta printed twice)

You need to store your optimal results in extra variables, e.g. Theta_opt, dif_opt etc. :

function[ theta_opt, dif_opt ] = targetangle(vs,d,e)

 % Input the initial condition:

 th = 1:0.1:45;

 k = 0.2;

 g = 9.81;

 dt = 0.001;

 u = 0:.1:599;

 v = 0:.1:599;

 theta_opt = 1;

 dif_opt = 100;

 for theta=th 

     the = theta * pi/180.;

     u(1) = vs * cos(the);

     v(1) = vs * sin(the);

     % Launch pad location:

     x(1) = 0.;

     y(1) = 0.;

     for t=1:1:6000;

         u(t+1) = u(t)- dt * (k * sqrt(u(t)^2+v(t)^2) * u(t));

         v(t+1) = v(t)- dt * (k * sqrt(u(t)^2+v(t)^2) * v(t) + g);

         x(t+1) = x(t) + u(t) * dt;

         y(t+1) = y(t) + v(t) * dt;

         % Determination of when the object hits ground:

         if y(t+1) < 0

             dif = min(abs(x(t+1) - d));

             if(dif < dif_opt)
                 theta_opt = theta;
                 dif_opt = dif;
                 x_opt = x;
                 y_opt = y;
             end

             break

         end

         % Once object hits terminate the computations with a break:

         if y(t+1) < 0; 

             break

         end

     end

 end

 plot(x_opt, y_opt);

 if dif_opt < e

     disp(dif_opt)

 else

     theta_opt = NaN;

 end

Upvotes: 1

Related Questions