Question

How can I get my while loop to run until my condition in my if statement...

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\n',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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

%the script now asks for input as long as the target is not hit

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\n',wind);
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
k=1;
while k==1
v0= input('Initial velocity(m/s): ');
a= input('Angle of projection(in degrees): ');
a=a*pi/180;
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!')
k=0;
break
end
end
end

Add a comment
Know the answer?
Add Answer to:
How can I get my while loop to run until my condition in my if statement...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MATLAB only please I am trying to get my getdatafuntion to work. I am also trying to get all my x...

    MATLAB only please I am trying to get my getdatafuntion to work. I am also trying to get all my x's, y's, and v's to popup in the command window so I can put in any value and it will find the value for me using the equation I put in. function project_9_sjl() % PROJECT_9_SJL project_9_sjl() is the driver function for the program. % %    Name: Scott Lawrence %   Date: 3/27/2019 %   Class: CMPSC 200 %   Description: Determine the optimal...

  • MATLAB code for a double pendulum. Please explain each lines for these codes pls. -------------------------------------...

    MATLAB code for a double pendulum. Please explain each lines for these codes pls. ---------------------------------------------------------------------------- clc close all clear all %---------Parameters------------------------------------------------------ L1=1; L2=1 ; M_1=2 ; M_2=1; G=9.8; %---------initial condition----------------------------------------------- tspan=30; theta1=3; theta1_prime=0; theta2=2.5; theta2_prime=0; y0=[theta1 theta1_prime theta2 theta2_prime]; [t,y]=ode45(@pend, [0 ,tspan],[ 3 0 2 0]); %---position of mass 1 and mass 2---------------------------------------- x1=L1*sin(y(:,1)); y1=-L1*cos(y(:,1)); x2=L1*sin(y(:,1))+l2*sin(y(:,3)); y2=-L1*cos(y(:,1))-l2*cos(y(:,3)); %------visualizing the result--------------------------------------------- figure(1) plot(x1,y1,'linewidth',2) hold on plot(x2,y2,'r','linewidth',2) h=gca; get(h,'fontSize') set(h,'fontSize',14) xlabel('X','fontSize',14); ylabel('Y','fontSize',14); title('Chaotic Double Pendulum','fontsize',14) fh = figure(1); set(fh, 'color', 'white'); figure(2)...

  • I need to create a loop that will run until an appropriate tank radius has been...

    I need to create a loop that will run until an appropriate tank radius has been found that will hold the liquid under these conditions. The program needs to display the radius as an answer in the command window. Thank you clear all,clc r = 1; % radius (m) h = 10; % height (m) F = 15; % flowrate (m^3/min) t = 120; % time (min) V_tank = pi * r^2 * h; V_liq = F * t; if...

  • I have all of the answers to this can someone just actually explain this matlab code and the results to me so i can get a better understanding? b) (c) and (d) %% Matlab code %% clc; close all; clear...

    I have all of the answers to this can someone just actually explain this matlab code and the results to me so i can get a better understanding? b) (c) and (d) %% Matlab code %% clc; close all; clear all; format long; f=@(t,y)y*(1-y); y(1)=0.01; %%%% Exact solution [t1 y1]=ode45(f,[0 9],y(1)); figure; plot(t1,y1,'*'); hold on % Eular therom M=[32 64 128]; T=9; fprintf(' M Max error \n' ); for n=1:length(M) k=T/M(n); t=0:k:T; for h=1:length(t)-1 y(h+1)=y(h)+k*f(t(h),y(h)); end plot(t,y); hold on %%%...

  • How would I be able to get a Merge Sort to run in this code? MY...

    How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private:    float array[1000] ;    int n = 1000;    int i=0; public:    void fillArray()    {        for(i=1;i<=n;i++)        {            array[i-1]= ( rand() % ( 1000) )+1;        }    }    void arrayout ()    {   ...

  • Matlab-How can i exit the for loop when counts exceeds the max value, then continue to...

    Matlab-How can i exit the for loop when counts exceeds the max value, then continue to the last loop and plot accepted values. counts = 0; for k = 2:length(partDiameter) overlaps = 1; while overlaps == 1 counts = counts + 1; if counts >= 10000 disp('Max particles packed') inCoord = [inCoord;[x,y]]; return; end x = (partDiameter(k)-L)*rand(1,1) + (L-partDiameter(k)/2); y = (partDiameter(k)-L)*rand(1,1) + (L-partDiameter(k)/2); % check if this x,y postion overlaps with previously packed % partices for m = 1:length(inCoord(:,1))...

  • Euler's Method C Get Homework Help With Sy solve for C, 2--(9e (1.71(0)+C) CUse Euler's Method To Make A T x +...

    Euler's Method C Get Homework Help With Sy solve for C, 2--(9e (1.71(0)+C) CUse Euler's Method To Make A T x + Cheg X x https://www.webassign.net/web/Student/Assignment-Responses/last?dep-21259636 Question Details LarCalc11 6 R014 My Notes k Your Teacher Use Euler's Method to make a table of values r the approximate solution of the differential equation with the specified initial value. Use n steps of size h. (Round your answers to four decimal places.) y' 7x-4y, y(0)-4, n-10, h-0.1 3 4 5 6...

  • using matlab thank you 3 MARKS QUESTION 3 Background The van der Pol equation is a 2nd-order ODE that describes self-sustaining oscillations in which energy is withdrawn from large oscillations and fe...

    using matlab thank you 3 MARKS QUESTION 3 Background The van der Pol equation is a 2nd-order ODE that describes self-sustaining oscillations in which energy is withdrawn from large oscillations and fed into the small oscillations. This equation typically models electronic circuits containing vacuum tubes. The van der Pol equation is: dt2 dt where y represents the position coordinate, t is time, and u is a damping coefficient The 2nd-order ODE can be solved as a set of 1st-order ODEs,...

  • How do I get my output to print "What class of car do you want" after every loop? Here is my outp...

    How do I get my output to print "What class of car do you want" after every loop? Here is my output, it gets stuck on asking and calculating the cost of the specific class of car i entered previously. clear cic: cars-input ('How many cars do you want to rentln run input ('Would you like to run the program Y or N','S) while run'Y carclass-input ('What class of car do you want B, C, or DIn', 'S while carclassB...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT