MATLAB Answers

What is wrong with my outputs?

44 views (last 30 days)
Ethan Wong Yew Hoe
Ethan Wong Yew Hoe on 31 Jul 2020 at 8:07
Commented: Ethan Wong Yew Hoe on 2 Aug 2020 at 17:40
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 ===================================%
N = 3; % Proportionality constant
Td = 30; % designated impact time
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;
%================ Plot x and Y values wrt to angle changes ===============%
grid on
xlabel('x displacement (m)');
ylabel('y displacement (m)');


Show 1 older comment
Ethan Wong Yew Hoe
Ethan Wong Yew Hoe on 31 Jul 2020 at 9:20
Hey there, thanks for the reply. Do you mean that i should pass initial values of xo and yo into the while loop? e.g
x=x0; y=y0;
while x ~= targetx & y ~= targety
my odefcn is as follows:
function dydx = odefcn(x, y, a, v)
dydx = zeros(2, 1);
dydx(1) = y(2);
dydx(2) = a/v^2;
Serhii Tetora
Serhii Tetora on 31 Jul 2020 at 9:37
and if you need only to plot y displasment, you need only first column of y, second column is derivative dy/dx
Ethan Wong Yew Hoe
Ethan Wong Yew Hoe on 2 Aug 2020 at 17:40
Hey there, thanks for the tip. My main aim is to plot the displacement of x against displacement of y. I have passe don values of x and y into the while loop. However, something illogical is going on in the graph shown below (i.e the target is now at coordinates 5, -5 ?? ) i have made sure that the target coordinates are at [5, 5] and that the loop only stops when final x and y value is equal to the target coordinates.
Still pulling my hair over this seemingly small but weird error!

Sign in to comment.

Answers (0)