필터 지우기
필터 지우기

How can I run multiple for loops in one function?

조회 수: 13 (최근 30일)
Zachary Giovanelli
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.

채택된 답변

Sri Harish G
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
  댓글 수: 1
Zachary Giovanelli
Zachary Giovanelli 2018년 6월 28일
I love it! So is table() a method then? Also what does temp. do? Also could you possibly give me a breakdown of what each part of the code is doing at these two lines:
temp.(['velocity_' num2str(v)])=[Height,Range];
temp.Properties.RowNames=cellstr(num2str(Angle));
Like what is Properties. and I'm assuming num2str is a method to take a number and make it a String text? Also lastly why did you do:
Angles=Angles(a)
What does that mean and do exactly? I apologize if this is asking a lot! I understand if you don't want to hassle with responding!
Thanks either way for the answer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Polar Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by