Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Plot two variables as a function of the third one

  1. Aug 29, 2010 #1

    I want to plot a curve with variables along the X & Y axis.
    But I also want the plot to show multiple curves as a function of another third variable.
    For example, if I'm plotting a curve of fruits vs. cost, I would get one curve.
    But how to plot it for say, seven days of the week, thus showing seven curves in the same graph.
    I would be doing this Matlab.
    So if someone could guide me with the codes.
    Hoping for a reply.

    Prasad M. Rao.
  2. jcsd
  3. Aug 29, 2010 #2
    You will get three dimensional graph since three variables are involved.
    you can use 3d plot feature in Matlab to do that
  4. Aug 29, 2010 #3
    No I don't want the graph to be 3D, I want it to be 2D but consist of multiple curves, the multiplicity being a function of the third variable for example, 7 curves for 7 days of the week.
  5. Aug 29, 2010 #4
    Ok then you can do like this

    Here y1 is array of costs of all fruits on day 1 y2 cost on day 2 etc.,
  6. Aug 29, 2010 #5
    Hey n.karthick thanks for replying so quickly.

    Thanks for the advice, but here the variables x, y & z are columns of data with more than a thousand readings., how do I work around that ??
    Can you post a sample code in Matlab.

    Prasad M. Rao.
  7. Aug 30, 2010 #6
    Let us assume the relation between 1st variable and 2nd variable in day 1 as
    and in day 2 as
    You can store 2nd variable in day1 in the variable y1 and day2 in y2
    so y1=2*x (day1) and y2=2.5*x (day2) and so on.
    Now you can plot 2D curve for all days like this

    As you mentioned if number of days are many you can do like this

    x=[2 3 ]; % enter values of x
    for i=1:n % n=no of days
    for j=1:m %length of x
    if i==1 % day 1 relation
    if i==2 % day 2 relation
    hold on % This will hold the current figure and plot the next values in same figure
  8. Aug 30, 2010 #7
    Hey n.karthick, once again thanks for replying.
    The relation that you suggested is very very difficult in my case, since I'm dealing with raw data and curve fitting is very difficult in my case and I will lose the essence of the data entirely.
    Somebody elsewhere suggested the 'unique' and 'sort' functions.
    Can you help me out with those codes.
    I'm kinda new to Matlab codes :(

    Prasad M. Rao.
  9. Sep 1, 2010 #8
    What I meant is you can store independent variable as a column vector say x and other two variables as a two dimensional array say y, in which first column of y represents second variable and second column contains third variable. So you can plot II and III variable as a function of I variable. (This way there is no need for curve fitting). Is there any difficulty in this method? If you can explain your problem in detail you may get answer
  10. Sep 18, 2010 #9
    Hey n.kartick I really need your help.
    Apologies for the delay.
    Lets see how helpful I cane be in my problem.
    I have three variables say X, Y and Z.
    I need to plot X vs. Y.
    Now the problem:
    1. Y is changing, so is Z.
    So now what I need is the graph of X vs. Y at different Z.
    2. Now all the variables (X,Y & Z) have thousands of values in them, but I would like the graphs at just some discrete points of Z.
    3. OK I know that the graph of X vs. Y is a curve. So now all I want is multiple graphs on the same page of X vs. Y at different Z.
    The people who know the exact code, said that I needed to use one or two for loops, the plot and the hold commands.
    Please need help on this.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook