Task: Create a flow chart to solve the task presented in section 3.2
A flow chart is a simplified description of a program used to organize the order of commands to be done. There are simple (and elaborate) ways to create a flow chart for a program. It is best to start with a simple structure and make the procedure more complicated as your programming effort expands.
The simplified descriptions of the tasks are not MATLAB commands, but are rather human language descriptions of the steps to take to get from the beginning to the end of the program that you want to write. In some places, these commands are called "pseudo-code" because they have the general character of the actual commands but don't include all of the required details.
Why even bother? Well, MATLAB is a sequential language which means you have to write your commands in the order in which you want them to be executed. You need to define variables (and their values) before you can use them. As your scripts become more complicated, you need to make sure that all of the actions (commands) are done in the right order. This is particularly true if you use logic to control the order that calculations are done (more about logical control later).
The flow chart can be done on a piece of paper, or in graphics software or in any number of ways. A useful procedure is to start with a MATLAB script and write the flow chart as a sequence of comments. When the plan is correct, add the MATLAB commands to the script, leaving the comments in the script as guideposts to understanding the script.
A benefit of this procedure is that the script with only the flow chart can be executed by MATLAB. Since it has only comments, it will not do anything. Then the individual steps can be added one group at a time. You can run the script as you add parts to your code to make sure it is doing what you want.
An example should show how this works. Consider a flow chart for the task presented in Task 3.2 of this lesson. The basic request is to create some vectors and make a plot. The flow chart might proceed as follows:
% Task 3.2 % define parameters % calculate time and y values % create a graphics window % draw the plot % add labels to the plot % save the plot to a fileThis seems to be a reasonable plan.
The first step in creating the script is
% Task 3.2 % define parameters Tmax=1000; Tstep=5; Decay=.001; Period=365; % calculate time and y values % create a graphics window % draw the plot % add labels to the plot % save the plot to a fileYou could run this script and see that the parameters are correctly define (and that the script runs!).
The next step is
% Task 3.2 % define parameters Tmax=1000; Tstep=5; Decay=.001; Period=365; % calculate time and y values time=0:Tstep:Tmax; y=exp(-Decay*time).*cos(2*pi*time/Period).^2; % create a graphics window % draw the plot % add labels to the plot % save the plot to a fileAgain, run this script and see that time and y arrays have been created.
Continue in this fashion until the script is complete and the answers are correct.