How can I run multiple for loops in one function?
17 ビュー (過去 30 日間)
古いコメントを表示
Zachary Giovanelli
2018 年 6 月 27 日
コメント済み: Zachary Giovanelli
2018 年 6 月 28 日
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.
0 件のコメント
採用された回答
Sri Harish G
2018 年 6 月 28 日
You can make an array with the 3 different angles and run a nested for loop then store the values in a table.
So your code would look something like:
function out = catapult(InitialV)
Angles=[30 35 45];
out=table();
for a=1:3
Angle=Angles(a);
temp=table();
for v = InitialV:10
% Calculate Height and Range using the same formulae
temp.(['velocity_' num2str(v)])=[Height,Range];
temp.Properties.RowNames=cellstr(num2str(Angle));
end
%Now add this row to the output table;
out=[out;temp];
end
end
This will return a Table Where each entry in the table is two values Height and Range
If initialV=8 then the output would look like
out =
3×3 table
velocity_8 velocity_9 velocity_10
__________________ ________________ ________________
30 0.81633 5.6557 1.0332 7.158 1.2755 8.837
35 1.0743 6.1368 1.3596 7.7668 1.6785 9.5887
45 1.6327 6.5306 2.0663 8.2653 2.551 10.204
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Polar Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!