Question: How can I get my while loop to run until my condition in my if statement is met? (In MATLAB) clc%clears screen clear all%clears history
How can I get my while loop to run until my condition in my if statement is met? (In MATLAB)
clc%clears screen
clear all%clears history
close all%closes all files
format long
%Euler's Method
%Initial conditions and setup
%Projectile motion
x=3;%randi(3);
y=3;%randi(3);
figure('color','white');
plot(x,y,'bo');
hold on
wind = (-10 + 20*rand(1));
fprintf('The wind speed is %2.2i ',wind);
v0= input('Initial velocity(m/s): ');
a= input('Angle of projection(in degrees): ');
a=a*pi/180;
h=.01;
cd=.1;
%cdy=.1;
%cdx=(rand*2-1)/2;
%cdy=(rand*2-1)/2;
m=1.5;
g=9.8; %acceleration due to gravity in m/s^2
%td=2*v0*sin(a)/g; %total time
x1=0;
y1=0;
t=0;
r=4;
while y1 >= 0 %y1 >=0
plot(x1,y1,'o','MarkerFaceColor','b','MarkerSize',8)
hold all
xlabel('Distance in meter');
ylabel('Height in meter');
xlim([0 30]);
ylim([0 30]);
getframe;
fd=(v0*cos(a))*cd+(wind);
x1=x1+(h*(v0*cos(a)+ fd*t));%(cd*t + wind))); %Euler's method for x
y1=y1+h*(v0*sin(a)-g*t-(cd/m)*t); %Euler's method for y
t=t+h;
if abs(x1-x) <= .25 && abs(y1-y) <= .25
fprintf('Congratulations you hit the target!')
break
end
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
