# Plot two variables as a function of the third one

1. Aug 29, 2010

### XLR8RPM

Hello,

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.

Thanks,

2. Aug 29, 2010

### n.karthick

You will get three dimensional graph since three variables are involved.
you can use 3d plot feature in Matlab to do that

3. Aug 29, 2010

### XLR8RPM

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.

4. Aug 29, 2010

### n.karthick

Ok then you can do like this
plot(x,y1,x,y2,x,y3)

Here y1 is array of costs of all fruits on day 1 y2 cost on day 2 etc.,

5. Aug 29, 2010

### XLR8RPM

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.

Thanks,

6. Aug 30, 2010

### n.karthick

Let us assume the relation between 1st variable and 2nd variable in day 1 as
y=2*x^2
and in day 2 as
y=2.5*x^3
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
plot(x,y1,x,y2....)

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

x=[2 3 ]; % enter values of x
n=2;
m=2;
for i=1:n % n=no of days
for j=1:m %length of x
if i==1 % day 1 relation
y(i,j)=2*x(j)^2;
end
if i==2 % day 2 relation
y(i,j)=2.5*x(j)^3;
end
end
plot(x,y(i,
hold on % This will hold the current figure and plot the next values in same figure
end

7. Aug 30, 2010

### XLR8RPM

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 :(

Thanks,

8. Sep 1, 2010

### n.karthick

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

9. Sep 18, 2010

### XLR8RPM

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.