Ask your own question, for FREE!
Mathematics 18 Online
OpenStudy (raffle_snaffle):

matlab help

OpenStudy (raffle_snaffle):

%% 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.

OpenStudy (raffle_snaffle):

OpenStudy (phi):

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 } }

OpenStudy (raffle_snaffle):

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

jimthompson5910 (jim_thompson5910):

why are you using an input to get the averages? why not just print out all the averages for each year?

OpenStudy (raffle_snaffle):

if I did that I wouldn't need a while loop?

OpenStudy (raffle_snaffle):

I could ask the user for 7 inputs and fprintf out 7 outputs

jimthompson5910 (jim_thompson5910):

is your instructions telling you to use input prompts?

OpenStudy (raffle_snaffle):

no, but I want to use input prompts.

jimthompson5910 (jim_thompson5910):

ok let me try something

OpenStudy (raffle_snaffle):

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.

jimthompson5910 (jim_thompson5910):

you'd have to put `elevationWater = input('Please enter a year: ');` somewhere in the "while" loop

OpenStudy (raffle_snaffle):

Oh, I didn't know that has to go inside the while loop. I thought it would go on the outside.

jimthompson5910 (jim_thompson5910):

`I thought it would go on the outside.` if you want to run it once, then yeah it goes outside the loop

jimthompson5910 (jim_thompson5910):

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 ```

jimthompson5910 (jim_thompson5910):

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

jimthompson5910 (jim_thompson5910):

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)');

OpenStudy (raffle_snaffle):

I just want the loop to run until I reach 2007 though.

OpenStudy (raffle_snaffle):

I want it to ask "Please enter a year?" Enter:2000 break repeat the question "Please enter a year?" Enter:2001 and so fourth

OpenStudy (raffle_snaffle):

forth*

jimthompson5910 (jim_thompson5910):

that seems way too tedious for both the programmer and the user

OpenStudy (raffle_snaffle):

lol okay, I will take your word for it.

OpenStudy (raffle_snaffle):

I am trying something out

jimthompson5910 (jim_thompson5910):

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); ```

jimthompson5910 (jim_thompson5910):

No user input is required. You just run the script m file and it will show you the 8 averages

OpenStudy (raffle_snaffle):

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".

jimthompson5910 (jim_thompson5910):

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

jimthompson5910 (jim_thompson5910):

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

OpenStudy (raffle_snaffle):

Okay I am going to play around with that for a bit just for my own benefit.

jimthompson5910 (jim_thompson5910):

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 ```

jimthompson5910 (jim_thompson5910):

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

OpenStudy (raffle_snaffle):

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.

jimthompson5910 (jim_thompson5910):

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

jimthompson5910 (jim_thompson5910):

You may need to include extra instructions about only typing in those years. Nothing else

OpenStudy (raffle_snaffle):

Okay I am going to work on the code above.

jimthompson5910 (jim_thompson5910):

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

jimthompson5910 (jim_thompson5910):

what do you mean?

OpenStudy (raffle_snaffle):

So you may need to go through that vector and find the average how would I go about finding the averages?

jimthompson5910 (jim_thompson5910):

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 ```

OpenStudy (raffle_snaffle):

Okay so when I run my code it just repeats the prompt. It doesn't calculate the average for year 2000

jimthompson5910 (jim_thompson5910):

attach your m file for me to look at

jimthompson5910 (jim_thompson5910):

I'm curious where you placed the average function @raffle_snaffle

OpenStudy (raffle_snaffle):

You have to scroll down to the bottom.

jimthompson5910 (jim_thompson5910):

Problem 9.17 ?

OpenStudy (raffle_snaffle):

yes

jimthompson5910 (jim_thompson5910):

your 'end' for the 'if' block is located in the wrong spot

jimthompson5910 (jim_thompson5910):

see attached

OpenStudy (raffle_snaffle):

it doesn't want to hook up with the end if I put it back up near the 'break'

jimthompson5910 (jim_thompson5910):

what do you mean?

OpenStudy (raffle_snaffle):

The little line that shows up between the if and end

jimthompson5910 (jim_thompson5910):

this is what your code should look like. All I did was move the first "end" up

jimthompson5910 (jim_thompson5910):

only one line of code should be in the "if" block

OpenStudy (raffle_snaffle):

hold on here

OpenStudy (raffle_snaffle):

for some reason the if can't see the end.

jimthompson5910 (jim_thompson5910):

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

jimthompson5910 (jim_thompson5910):

it can't see the end? how strange

jimthompson5910 (jim_thompson5910):

you may need to restart matlab if it's being buggy

OpenStudy (raffle_snaffle):

nvm about my last comment, about the line showing up there is no line.

jimthompson5910 (jim_thompson5910):

so everything is working now?

OpenStudy (raffle_snaffle):

no everything is not working

OpenStudy (raffle_snaffle):

jimthompson5910 (jim_thompson5910):

you set up k after you use it. You're supposed to set up k BEFORE you use it

jimthompson5910 (jim_thompson5910):

I'm referring to this portion ``` % find the average per year avg(k) = mean(data(:, k)); % sets up proper index k = 2000 - 2000 + 1; ```

OpenStudy (raffle_snaffle):

Okay I actually put the k before and still doesn't run like it should

jimthompson5910 (jim_thompson5910):

and k will always be the same. You didn't put any variables in the k equation

jimthompson5910 (jim_thompson5910):

k will always be 1 if you put `k = 2000 - 2000 + 1;`

OpenStudy (raffle_snaffle):

I see what you are saying. thinking

jimthompson5910 (jim_thompson5910):

btw, why did you make the input name to be "elevationWater" ? why not call it "year" ?

OpenStudy (raffle_snaffle):

jimthompson5910 (jim_thompson5910):

much better

OpenStudy (raffle_snaffle):

So does your k equations always relate to your constraint for the "while"?

jimthompson5910 (jim_thompson5910):

what do you mean?

OpenStudy (raffle_snaffle):

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

OpenStudy (raffle_snaffle):

Well, it still doesn't run. It just repeated the loop by asking user to enter a value over ang over again.

jimthompson5910 (jim_thompson5910):

`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

OpenStudy (raffle_snaffle):

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)

jimthompson5910 (jim_thompson5910):

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

OpenStudy (raffle_snaffle):

^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

jimthompson5910 (jim_thompson5910):

ctrl+c forces the program to shut down? or it pauses?

OpenStudy (raffle_snaffle):

it stop the loop.

OpenStudy (raffle_snaffle):

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

jimthompson5910 (jim_thompson5910):

alright

OpenStudy (raffle_snaffle):

Does it run on your end?

jimthompson5910 (jim_thompson5910):

yes it works for me

jimthompson5910 (jim_thompson5910):

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

OpenStudy (raffle_snaffle):

Yes, that works. So in order to see results user has to enter 0

jimthompson5910 (jim_thompson5910):

yes if you use the cntrl+c method, then the script never finishes to the bottom

OpenStudy (raffle_snaffle):

I guess when you input multiple years before exiting it averages all the years?

OpenStudy (raffle_snaffle):

It doesn't average each year individually.

OpenStudy (raffle_snaffle):

nvm I think I have spent long enough on this problem and need to start working on my lab.

jimthompson5910 (jim_thompson5910):

like I said, it's better to make a for loop and forget about user input

jimthompson5910 (jim_thompson5910):

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); ```

OpenStudy (raffle_snaffle):

^yeah I would agree.

jimthompson5910 (jim_thompson5910):

wait, I forgot about averaging the averages. Let me fix

jimthompson5910 (jim_thompson5910):

``` 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); ```

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!