How can I run multiple for loops in one function?
Mostra commenti meno recenti
Here is my code:
function [Height,Range] = catapult(InitialV)
% calculates at Angle 30 degrees
Angle = 30;
fprintf('For an Angle = 30 Degrees\n')
for v = InitialV:10
% Used to find Velocity in Y direction
Vyinitial = (v * sind(Angle));
% Used to find the total time
Tfinal = ((2*v * sind(Angle))/9.8);
% Used to find time for maximum height
Time = Tfinal/2;
% Used to find max height
Height = Vyinitial * Time - (.5 * 9.8 * Time^2);
% Use to find velocity in x direction
Vxinitial = (v * cosd(Angle));
% Used to find total distance
Range = Vxinitial * Tfinal;
fprintf('%3.2f %6.2f\n', Height,Range)
end
%calculates at angle 35 degrees
Angle = 35;
fprintf('For an Angle = 35 Degrees\n')
for v = InitialV:10
% Used to find Velocity in Y direction
Vyinitial = (v * sind(Angle));
% Used to find the total time
Tfinal = ((2*v * sind(Angle))/9.8);
% Used to find time for maximum height
Time = Tfinal/2;
% Used to find max height
Height = Vyinitial * Time - (.5 * 9.8 * Time^2);
% Use to find velocity in x direction
Vxinitial = (v * cosd(Angle));
% Used to find total distance
Range = Vxinitial * Tfinal;
fprintf('%3.2f %6.2f\n', Height,Range)
end
%calculates at angle 45 degrees
Angle = 45;
fprintf('For an Angle = 45 Degrees\n')
for v = InitialV:10
% Used to find Velocity in Y direction
Vyinitial = (v * sind(Angle));
% Used to find the total time
Tfinal = ((2*v * sind(Angle))/9.8);
% Used to find time for maximum height
Time = Tfinal/2;
% Used to find max height
Height = Vyinitial * Time - (.5 * 9.8 * Time^2);
% Use to find velocity in x direction
Vxinitial = (v * cosd(Angle));
% Used to find total distance
Range = Vxinitial * Tfinal;
fprintf('%3.2f %6.2f\n', Height,Range)
end
end
My question is I would like to have the same loop be able to go through the three different angles listed and then also go through the "InitialV" input variable to 10. I would also like it to output a matrices like table where it has the 3 different angles listed in a column and then the different 'V' velocity in a top row and then the data in the table would be the different Heights and Ranges in the matrices table. Pic of table.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!