Results 1 to 4 of 4

Math Help - Matlab looping/plotting problem

  1. #1
    Newbie
    Joined
    Feb 2009
    Posts
    3

    Matlab looping/plotting problem

    Hey all,

    I have a script written to predict elevation in two loops. One ("small loop") calculates elevation over fifty timesteps, and creates records of all the necessary data (elevation, etc.). The second ("large loop") contains the small loop, runs twenty times, and plots the records after each set of 50 iterations of the small loop. The large loop wipes the records clean at the beginning of each of its 20 iterations, and creates new, empty records and defines all the relavent variables before the small loop starts.

    The script works fine, I get 20 unique elevation records of 50 timesteps each, but the plot only ever records two records at a time, and then it wipes itself clean and plots the next two, before wiping itself clean again (i.e. it plots iterations 1 and 2 of the large loop, then iterations 3 and 4, 5 and 6, and so on, but never more than two at once). I've checked my hold on command, and it seems to be placed properly -- I have one at the end of the plotting commands, which are in the large loop, but after the small loop.

    So basically I need to figure out why it won't plot more than two lines at once, and then to figure out how to plot all twenty lines on the same plot.

    Help would be appreciated!

    Thanks in advance,

    -Brad
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by bp023 View Post
    Hey all,

    I have a script written to predict elevation in two loops. One ("small loop") calculates elevation over fifty timesteps, and creates records of all the necessary data (elevation, etc.). The second ("large loop") contains the small loop, runs twenty times, and plots the records after each set of 50 iterations of the small loop. The large loop wipes the records clean at the beginning of each of its 20 iterations, and creates new, empty records and defines all the relavent variables before the small loop starts.

    The script works fine, I get 20 unique elevation records of 50 timesteps each, but the plot only ever records two records at a time, and then it wipes itself clean and plots the next two, before wiping itself clean again (i.e. it plots iterations 1 and 2 of the large loop, then iterations 3 and 4, 5 and 6, and so on, but never more than two at once). I've checked my hold on command, and it seems to be placed properly -- I have one at the end of the plotting commands, which are in the large loop, but after the small loop.

    So basically I need to figure out why it won't plot more than two lines at once, and then to figure out how to plot all twenty lines on the same plot.

    Help would be appreciated!

    Thanks in advance,

    -Brad
    Can we see the code?

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Feb 2009
    Posts
    3
    Sure -- the calls to other scripts (i.e. volume, elev_area) are just functions written in other files. And sorry in advance that all the justification is thrown off.




    % MONONEW - script to predict the water level in Lake Mono, CA
    % Updated 21 February by Brad Peters

    clear %clear existing workspace variables



    %define timespace variables

    time_max = 10; %total time for simulation (yrs)
    dt = 1; %simulation time interval (yrs)

    %set imax

    jmax = time_max/dt; %sets the maximum number of iterations for the model

    for j = 1:jmax

    clear %clear existing workspace variables

    %statistical data for inputs
    mu_q_in = 167.47;
    sigma_q_in = 53.19;
    mu_p = 0.66;
    sigma_p = 0.25;
    mu_e = 3.58;
    sigma_e = 0.26;

    % initial parameters

    r = randn(50,1); %random number generation from a (0,1) std nml distrib
    e0 = 6374; %initial elev (ft)
    v0 = volume(e0); %initial volume calculation (1000 acre - ft)
    a0 = elev_area(e0); %initial area calculation (1000 acres)

    %create datasets from initial parameters

    volume_mono = [v0]; %creates variable volume_mono, starting with v0 (1000 acre-ft)
    area_mono = [a0]; %creates variable area_mono, starting with a0 (1000 acres)
    elev_mono = [e0]; %creates variable elev_mono, starting wtih e0 (ft)

    %create empty record datasets

    volume_record = []; %creates empty record dataset for volume
    area_record = []; %creates empty record dataset for area
    elev_record = []; %creates empty record dataset for elevation
    q_record = []; %creates empty record dataset for overland flows
    p_record = []; %creates empty record dataset for precipitation
    e_record = []; %creates empty record dataset for evaporation
    d_record = []; %creates empty record dataset for diversions
    e_t_record = []; %creates empty record dataset for target elevation

    %clear figure

    figure(1) %calls figure 1

    %define timespace variables

    time_max_new = 50; %total time for simulation (yrs)
    dt_new = 1; %simulation time interval (yrs)

    %set imax

    imax_new = time_max_new/dt_new; %sets the maximum number of iterations for the model

    for i = 1:imax_new %starts calculation loop for i=1 through i=imax

    time = i*dt_new; %defines variable "time"

    %make datasets from excel calls or random number grabs

    q = (r(i,1)*sigma_q_in)+mu_q_in; %calculates overland flow levels from random number grab
    p_r = (r(i,1)*sigma_p)+mu_p; %calculates precipitation levels from random number grab
    e_r = -((r(i,1)*sigma_e)+mu_e); %calculates evaporation levels from random number grab
    e_t = 6392; %defines target elevation

    %function calls

    area_mono = area(volume_mono); %calculates lake area given lake volume
    elev_mono = elevation(volume_mono); %calculates lake elevation given lake volume

    %volume of evap/precip

    v_p = p_r*area_mono; %calculates volume of precipitation given height of precipitation (1000 ac-ft/yr)
    v_e = e_r*area_mono; %calculates volume of evaporation given height of evaporation (1000 ac-ft/yr)

    %calculation loop

    if elev_mono >= 6380 %calculation only for given elevation range
    d_r = -16; %diversion quantity at this elevation range (1000 ac-ft/yr)
    volume_mono = volume_mono + q + v_p + v_e + d_r; %recalculates lake volume (1000 ac-ft)

    else if 6377 <= elev_mono < 6380 %calculation only for given elevation range
    d_r = -4.5; %diversion quantity at this elevation range (1000 ac-ft/yr)
    volume_mono = volume_mono + q + v_p + v_e + d_r; %recalculates lake volume (1000 ac-ft)

    else %calculation for all other elevations
    d_r = 0; %diversion quantity at this elevation range (1000 ac-ft/yr)
    volume_mono = volume_mono + q + v_p + v_e+ + d_r; %recalculates lake volume (1000 ac-ft)

    end
    end

    %record making

    volume_record = [volume_record;i volume_mono]; %fills in record for volume
    area_record = [area_record;i area_mono]; %fills in record for area
    elev_record = [elev_record;i elev_mono]; %fills in record for elevation
    q_record = [q_record;i q]; %fills in record for overland flows
    p_record = [p_record;i v_p]; %fills in record for precipitation
    e_record = [e_record;i v_e]; %fills in record for evaporation
    d_record = [d_record;i d_r]; %fills in record for diversions
    e_t_record = [e_t_record;i e_t]; %fills in record for target elevation level

    end

    %create figure

    figure(1) %calls figure 1
    title('Elevation and Flux of Lake Mono, Ca, 1994-2094') %titles figure 1

    subplot(2,1,1) %creates subplot 1 of 2
    title('Elevation of Lake Mono') %titles subplot
    plot(elev_record(:,1),elev_record(:,2),'-g') %plots elevation vs. time
    hold on %holds subplot for additional graph(s)
    plot(e_t_record(:,1),e_t_record(:,2),'-b') %plots target elevation vs. time
    axis([0,50,6370,6395]) %sets axis limits
    xlabel('Years after 1994') %labels x-axis
    ylabel('Elevation (ft)') %labels y-axis
    legend('Calculated Elevation','Target Elevation','Location','NorthEastOutside') %adds legend to subplot
    hold on %holds subplot for additional graph(s)

    subplot(2,1,2) %creates subplot 2 of 2
    title('Flux') %titles subplot
    plot(q_record(:,1),q_record(:,2),'-g',p_record(:,1),p_record(:,2),'-k',e_record(:,1),e_record(:,2),'-b',d_record(:,1),d_record(:,2),'-r')
    %prev line: plots overland flows, precipitation, evaporation, and
    %diversions vs. time
    axis([0,50,-300,300]) %sets axis limits
    xlabel('Years After 1994') %labels x-axis
    ylabel('Flux (1000 ac - ft)') %labels y-axis
    legend('Overland Inflows','Precipitation','Evaporation','Diversions ','Location','NorthEastOutside') %adds legend to subplot
    hold on %holds subplot for additional graph(s)

    end
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Feb 2009
    Posts
    3
    And the large loop is actually set on ten timesteps...not twenty.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. matlab plotting graph need help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: August 6th 2010, 11:27 PM
  2. plotting a graph in matlab
    Posted in the Math Software Forum
    Replies: 3
    Last Post: March 21st 2009, 02:11 PM
  3. Matlab - Plotting Circles
    Posted in the Math Software Forum
    Replies: 1
    Last Post: February 16th 2009, 08:28 AM
  4. MATLAB: Plotting MATRIX?
    Posted in the Math Software Forum
    Replies: 3
    Last Post: January 25th 2009, 12:21 PM
  5. MATLAB - plotting a quadratic
    Posted in the Math Software Forum
    Replies: 2
    Last Post: June 15th 2008, 05:25 PM

Search Tags


/mathhelpforum @mathhelpforum