Plot two variables as a function of the third one

AI Thread Summary
To plot multiple curves on a 2D graph in MATLAB, one can use the `plot` function with arrays representing different conditions, such as days of the week. Each curve can be defined by its own set of Y values corresponding to the X values, allowing for multiple plots on the same graph. For large datasets with thousands of readings, it is suggested to store the independent variable as a column vector and the dependent variables in a two-dimensional array. Utilizing loops can help in iterating through the data points for each condition, ensuring the plots are displayed correctly. The discussion emphasizes the need for clarity in data handling to avoid losing the essence of the raw data during plotting.
XLR8RPM
Messages
5
Reaction score
0
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.
Hoping for a reply.

Thanks,
Prasad M. Rao.
 
Physics news on Phys.org
You will get three dimensional graph since three variables are involved.
you can use 3d plot feature in Matlab to do that
 
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.
 
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.,
 
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,
Prasad M. Rao.
 
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
 
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,
Prasad M. Rao.
 
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
 
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.
Thanks!
 
Back
Top