44 views (last 30 days)

Hey all, I can't seem to find a specific error form my code. It came up with a diagram which doesn't make much sense to me. I'm designing a code that allows an object to home in on a target at given coordinates [5, 5].

What my graph showed was the end point of the object's trajectory to be at [5, -5] for reasons which I can;t seem to find. Attached is an excerpt of my code. Am i using the ode45 wrongly? Or is there a flaw in my syntax in general? My aim is to plot x displacement w.r.t to y displacment for the object launched.

Any tips/helps would be much appreciated, thanks in advance!

%========================= Explanation of code ===========================%

% Get launch angle (theta) from user, then get initial x and y coordinates

% Constants are VELOCITY (v), N, DESIGNATED TIME (Td)

% Variables are x and y

% LOS, a, L are always changing, so need to INCLUDE THEM IN A LOOP

% Output will be x and y, ALSO ALWAYS CHANGING

%======================= Get input from user =============================%

theta0 = input('Please input launch angle in degrees: ');

x0 = input('Please enter initial x coordinates: ');

y0 = input('Please enter initial y coordinates: ');

%=========================== Constants ===================================%

v=1600;

N = 3; % Proportionality constant

Td = 30; % designated impact time

L = 500; % NEED TO DERIVE

I = 205; % moment of inertia

targetx= 5; targety = 5; % I set targetcoordinates as [5, 5]

Rgo_x = targetx - x0;

Rgo_y = targety - y0;

%============== Loop to keep churning out x and y values =================%

theta = theta0;

xspan = [x0 targetx];

yspan = [y0 targety];

IC = [0 theta0];

while x ~= targetx & y ~= targety

Rgo = sqrt(Rgo_x^2 + Rgo_y^2); % Current range between missle and target

omega = v/Rgo;

LOS = L/(I*omega); %L = applied torque, I = moment of intertia of gyroscope, omega = v/Rgo %LOS rate

Tgo = (1+ (theta-acosd(Rgo_x/Rgo))/10)*(Rgo/v);

a = N*v*LOS - (60*v^5*(Td-Tgo))/(N*v*LOS*(Rgo^3));

theta = a*(Rgo_x)/v^2 + theta;

[x, y] = ode45(@(x, y)odefcn(x, y, a, v), xspan, yspan, IC);

Rgo_x = targetx - x;

Rgo_y = targety - y;

end

%================ Plot x and Y values wrt to angle changes ===============%

figure

grid on

plot(x,y)

xlabel('x displacement (m)');

ylabel('y displacement (m)');

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573277-what-is-wrong-with-my-outputs#comment_956107

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573277-what-is-wrong-with-my-outputs#comment_956107

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573277-what-is-wrong-with-my-outputs#comment_956116

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573277-what-is-wrong-with-my-outputs#comment_956116

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573277-what-is-wrong-with-my-outputs#comment_956122

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573277-what-is-wrong-with-my-outputs#comment_956122

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573277-what-is-wrong-with-my-outputs#comment_958198

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573277-what-is-wrong-with-my-outputs#comment_958198

Sign in to comment.