What is wrong with my outputs?
1 view (last 30 days)
Show older comments
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)');
6 Comments
Uday Pradhan
on 5 Aug 2020
Your while loop will not terminate when both x and y are 5, it will terminate when either x or y becomes 5. With the first execution itself x gets the value 5. You may try
while ~(x == 5 & y == 5)
Also I am not so sure about your arguments list in ode45. You may check the documentation first as the fourth option is for "options" structure array.
Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!