How to create a custom function using matlab and plot its flowchart

1 Ansicht (letzte 30 Tage)
angela lim
angela lim am 24 Okt. 2016
Beantwortet: Alexandra Harkai am 24 Okt. 2016
How to create a function that include control and flow commands, such as “if, switch, etc.” and loop commands such as “for and while”? The function should accept input arguments and output the solution.
  3 Kommentare
angela lim
angela lim am 24 Okt. 2016
Bearbeitet: Jan am 24 Okt. 2016
I have an idea like below, but I dont know how to write the code to make it run...
% This program will calculate the area and circumference of circles, allowing the radius as an input,but will only provide output for circles with an area that exceeds 20.
R = input;
while R > 0.0
AREA = pi * R^2;
CIRC = 2.0 * pi * R;
if AREA > 20.0
fprintf('\n Radius = %f units',R)
fprintf('\n Area = %f square units', AREA)
fprintf('\n Circumference = %f units\n', CIRC)
else
N = N + 1;
end
end
fprintf('\n Number of circles that do not have area > 20: %.0f \n', N)
Jan
Jan am 24 Okt. 2016
Bearbeitet: Jan am 24 Okt. 2016
@angela lim: I've formatted your code to make it readable. Please use the "{} Code" button in teh future. Thank you.
When you mention, that you cannot make it to run, please explain what happens instead. Do you get an error message or does teh result differ from your expectations? It is easier to solve a problem than to guess it.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jan
Jan am 24 Okt. 2016
The purpose of your code is not clear due to the absence of comments. Currently you ask for the input once only before the loop. Inside the loop you increase N (which seems to be undefined such that Matlab should stop with an error). But what should trigger the end of the loop?
A bold guess:
N = 0;
ready = false;
while ~ready
R = input('Type in the radius (<0 to stop the loop): ');
if R < 0
ready = true;
else
AREA = pi * R^2;
CIRC = 2.0 * pi * R;
if AREA >= 20.0 % >= instead of > to be exact
fprintf('\n Radius = %f units',R)
fprintf('\n Area = %f square units', AREA)
fprintf('\n Circumference = %f units\n', CIRC)
else
N = N + 1;
end
end
end
fprintf('\n Number of circles that do not have area > 20: %.0f \n', N)

Alexandra Harkai
Alexandra Harkai am 24 Okt. 2016
You are very close. With minimal changes to your code you can run something like this:
function CircleCalculator(input)
m = 1;
N = 0;
while input(m) > 0.0
R = input(m);
AREA = pi * R^2;
CIRC = 2.0 * pi * R;
if AREA > 20.0
fprintf('\n Radius = %f units',R);
fprintf('\n Area = %f square units', AREA);
fprintf('\n Circumference = %f units\n', CIRC);
else
N = N + 1;
end
m = m + 1;
end
fprintf('\n Number of circles that do not have area > 20: %.0f \n', N);
end
What was missing is to check each element of the input vector (input(m)), and sort of loop over all the m values from 1. Also N needs to have a starting value. Note this only works when you have a 0 element in input, because the while loop needs to stop at some point before you run out of input.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by