matlab help
%% Problem 9.17 %{ 8.10 The Colorado River Drainage Basi covers parts of seven western states. A series of dams has been constructed on the Colorado River and its tributaries to store runoff water and to generate low-cost hydroelectric power (see Figure P8.10). The ability to regulate the flow of water has made the growth of agriculture and popluation in these arid desert states possible. Even during the periods of extended drought, a steady, reliable source of water and electricity has been avaialble to the basin states. Lake Powell is one of these reservoirs. The file lake_powell.dat containts data on the water leve in the reservoir for the 8 years from 2000 to 2007. These data are shown in Table 8.11. Use the data in the file to answer the following questions: (a) Determine the average elevation of the water level for each year and for the 8-year period over which data were collected.
How about this: while (1) % loop forever { input year if year == 0 then break; end; % quit if you get 0 ix= year - 1999; % assuming the first year is 2000, this gives you ix=1 check that ix is legal if not legal complain else { do stuff } }
clear, clc % clears command workspace window % loads the mat file into matlab called data data = importdata('data.mat'); % average elevation of water per year % asking user what year would you like to find avg elevations of rainfall elevationWater = input('Please enter a year: '); yearMax = 7; k = 0; while (k <= yearMax) if (elevationWater == 2000) avg = mean(data(:, 1)); k = k + 1; elseif (elevationWater == 2001) avg = mean(data(:, 2)); k = k + 1; elseif (elevationWater == 2002) avg = mean(data(:, 3)); end k = k + 1; end fprintf('average elevation of rainfall is: %0.f \n', avg); @jim_thompson5910
why are you using an input to get the averages? why not just print out all the averages for each year?
if I did that I wouldn't need a while loop?
I could ask the user for 7 inputs and fprintf out 7 outputs
is your instructions telling you to use input prompts?
no, but I want to use input prompts.
ok let me try something
Why can't I run the loop over multiple times asking the user to input a new year? That is what I was trying to get the program to do.
you'd have to put `elevationWater = input('Please enter a year: ');` somewhere in the "while" loop
Oh, I didn't know that has to go inside the while loop. I thought it would go on the outside.
`I thought it would go on the outside.` if you want to run it once, then yeah it goes outside the loop
if you want to keep running the input indefinitely, then you'd have ``` while(1) elevationWater = input('Please enter a year: '); % if the user types in 0, then that means the user wants % to break out of the loop and end the program if(elevationWater == 0) break; end % do other stuff here (like find the avg) end ```
if you had "while(k < 10)" then the loop will keep running until "k<10" is false saying "while(1)" will keep the loop running forever since the input "1" is always true. Matlab treats "1" as true and "0" as false. So the loop will run forever but if the user types in 0, then the command "break" will be given to get out of the loop
I guess the user would have no idea entering 0 breaks out of the loop, so it's always a good idea to inform the user elevationWater = input('Please enter a year: (type 0 to quit)');
I just want the loop to run until I reach 2007 though.
I want it to ask "Please enter a year?" Enter:2000 break repeat the question "Please enter a year?" Enter:2001 and so fourth
forth*
that seems way too tedious for both the programmer and the user
lol okay, I will take your word for it.
I am trying something out
why not use a simple for loop? ``` clear, clc % clears command workspace window % loads the mat file into matlab called data data = importdata('data.mat'); %go through the years 2000 through 2007 % and compute the averages % k = 1 corresponds to the year 2000 % k = 8 corresponds to the year 2007 for k = 1:1:8 avg(k) = mean(data(:, k)); end fprintf('average elevation of rainfall is: %0.f \n', avg); ```
No user input is required. You just run the script m file and it will show you the 8 averages
If we used the while(1) and if (elevationWater == 0) break how would we go about finding the different columns to average or is this just too complicated since we have to use the columns separately in the matrix? I like the code you have above I am just trying to be more "creative".
say a user types in `2003` that means elevationWater = 2003 so I'd make k = elevationWater-2000+1 k = 2003-2000+1 k = 3+1 k = 4 the year 2003 corresponds to data column 4
since `k = 4`, this means `avg(k) = mean(data(:, k));` is the same as `avg(4) = mean(data(:, 4));` the 4th value in the "avg" vector is equal to the mean of the data in column 4
Okay I am going to play around with that for a bit just for my own benefit.
Honestly, I'd go with the option of not having user input. It seems like too much work. But if you insist on user input, then this is one way to do it ``` while(1) % get the year from the user elevationWater = input('Please enter a year: (type 0 to quit)'); % if the user types in 0, then that means the user wants % to break out of the loop and end the program if(elevationWater == 0) break; % get out of the "while" loop end % set up the proper index k = elevationWater-2000+1 % find the average avg(k) = mean(data(:, k)); % loop repeats back up at the top after this section % loop will stop if the user types in 0 % otherwise, the loop goes on forever end ```
sorry I forgot the bit of code after the "while" loop ``` while(1) % get the year from the user elevationWater = input('Please enter a year: (type 0 to quit)'); % if the user types in 0, then that means the user wants % to break out of the loop and end the program if(elevationWater == 0) break; % get out of the "while" loop end % set up the proper index k = elevationWater-2000+1 % find the average avg(k) = mean(data(:, k)); % loop repeats back up at the top after this section % loop will stop if the user types in 0 % otherwise, the loop goes on forever end fprintf('average elevation of rainfall is: %0.f \n', avg); ``` though that will spit out multiple averages and it won't be clear which averages they are
The one thing I struggle the most with when using while loops is finding the correct index. User has to make a proper formula to repeat the correct index.
When programming, always try to make the input instructions as clear as possible. The user may not know what to type in. So when you say "type in the year" expect the user to type in 2000,20001,etc all the way up to 2007
You may need to include extra instructions about only typing in those years. Nothing else
Okay I am going to work on the code above.
I'm realizing that after the "while" loop ends, the vector avg will have multiple values in it. So you may need to go through that vector and find the average
what do you mean?
So you may need to go through that vector and find the average how would I go about finding the averages?
if "avg" is a vector with all the averages (8 total for the 8 years) of the columns then you can use the `sum` function to sum up all the elements in the "avg" vector then divide by 8 ``` average = sum(avg)/8 ```
Okay so when I run my code it just repeats the prompt. It doesn't calculate the average for year 2000
attach your m file for me to look at
I'm curious where you placed the average function @raffle_snaffle
You have to scroll down to the bottom.
Problem 9.17 ?
yes
your 'end' for the 'if' block is located in the wrong spot
see attached
it doesn't want to hook up with the end if I put it back up near the 'break'
what do you mean?
The little line that shows up between the if and end
this is what your code should look like. All I did was move the first "end" up
only one line of code should be in the "if" block
hold on here
for some reason the if can't see the end.
if you had what you initially had, then that other code would only get executed if the user typed in 0. Try it out yourself
it can't see the end? how strange
you may need to restart matlab if it's being buggy
nvm about my last comment, about the line showing up there is no line.
so everything is working now?
no everything is not working
you set up k after you use it. You're supposed to set up k BEFORE you use it
I'm referring to this portion ``` % find the average per year avg(k) = mean(data(:, k)); % sets up proper index k = 2000 - 2000 + 1; ```
Okay I actually put the k before and still doesn't run like it should
and k will always be the same. You didn't put any variables in the k equation
k will always be 1 if you put `k = 2000 - 2000 + 1;`
I see what you are saying. thinking
btw, why did you make the input name to be "elevationWater" ? why not call it "year" ?
much better
So does your k equations always relate to your constraint for the "while"?
what do you mean?
So for example while (1) means were are running the loop until user enter '0' k = year - 2000 + 1; when I enter: 2000 we get k = 2000 - 2000 + 1 = 1 so this one get thrown... nevermind. It gets thrown into the avg(k) equation
Well, it still doesn't run. It just repeated the loop by asking user to enter a value over ang over again.
`while (1)` means the loop runs as long as "1" is true "1" is always true, so the loop runs forever however, we can override this and break out of the loop any time we want. In this case, we break out when the user types in 0
Please enter a year: (type 0 to quit)2000 Please enter a year: (type 0 to quit)2001 Please enter a year: (type 0 to quit)2002 Please enter a year: (type 0 to quit)2003 Please enter a year: (type 0 to quit)
if you did not have the break in there, the loop would run forever. It would run until you force the program to close, the program runs out of memory, or you unplug the computer or something
^That is what it does after i press enter it just keeps on prompting the user over and over again until I pres ctr+c
ctrl+c forces the program to shut down? or it pauses?
it stop the loop.
when you run the loop it propmps the user over and over again. To get out hold ctr and then press c in the command window
alright
Does it run on your end?
yes it works for me
I think when you hit ctrl+c, you are stopping the entire script which means you skip over the part where you show the averages to the user you want to type in 0 for the year to properly get out of the loop
Yes, that works. So in order to see results user has to enter 0
yes if you use the cntrl+c method, then the script never finishes to the bottom
I guess when you input multiple years before exiting it averages all the years?
It doesn't average each year individually.
nvm I think I have spent long enough on this problem and need to start working on my lab.
like I said, it's better to make a for loop and forget about user input
this is how I'd do it ``` clear, clc % clears command workspace window % loads the mat file into matlab called data data = importdata('data.mat'); %go through the years 2000 through 2007 % and compute the averages % k = 1 corresponds to the year 2000 % k = 8 corresponds to the year 2007 for k = 1:1:8 avg(k) = mean(data(:, k)); end fprintf('average elevation of rainfall is: %0.f \n', avg); ```
^yeah I would agree.
wait, I forgot about averaging the averages. Let me fix
``` clear, clc % clears command workspace window % loads the mat file into matlab called data data = importdata('data.mat'); %go through the years 2000 through 2007 % and compute the averages % k = 1 corresponds to the year 2000 % k = 8 corresponds to the year 2007 for k = 1:1:8 avg(k) = mean(data(:, k)); end average = sum(avg)/8; fprintf('average elevation of rainfall is: %0.f \n', average); ```
Join our real-time social learning platform and learn together with your friends!