Is there a way I can create one graph out of many other graphs? (i.e., a combo graph)
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
Hi guys,
Here's what I want to do.
I have 3 different formulas:
x = [-10:10];
y = [-10:10];
Formula1 = @(x,y) x + y;
Formula2 = @(x,y) 2.*x + 2.*y;
Formula3 = @(x,y) 3.*x + 2.*y;
But when I goto graph it, I just want to create one single curve.. where from point [-10, -10] to [-3, -3] formula1 is graphed... from point [-3,-3] to [5,5] formula2 is graphed.... from point [5,5] to [10,10] formula3 is graphed.
I also want them to be of different color.
Lastly, is there a way to generate a 'formula' from that single shape that is created?
Sorry for asking so many questions.
Thank you
Accepted Answer
Star Strider
on 11 Oct 2014
Unfortunately, you do not generate one single curve. The curves are discontinuous at their respective boundaries.
You specified single curves, not surfaces, so I went with plot3. The cell arrays make the (x,y) addressing efficient.
This is likely the best you can hope for:
x = {-10:-3; -3:5; 5:10};
y = x;
Formula1 = @(x,y) x + y;
Formula2 = @(x,y) 2.*x + 2.*y;
Formula3 = @(x,y) 3.*x + 2.*y;
F1 = Formula1(x{1},y{1});
F2 = Formula2(x{2},y{2});
F3 = Formula3(x{3},y{3});
c = colormap(jet(3));
figure(1)
plot3(x{1},y{1},F1, 'Color',c(1,:))
hold on
plot3(x{2},y{2},F2, 'Color',c(2,:))
plot3(x{3},y{3},F3, 'Color',c(3,:))
hold off
grid on
xlabel('X')
ylabel('Y')
zlabel('F(X,Y)')
Because of the discontinuities, there is no single function that can describe all three functions. Even a regression that ‘ignored’ the discontinuities and attempted to fit all the data would be difficult.
If you have R2014b, you can eliminate the colormap call and the 'Color',c() arguments to plot3, since R2014b now changes them on its own.
9 Comments
A
on 11 Oct 2014
Thank you. That is exactly what I was looking for.
Just for knowledge, basically, is there no way to 'connect' the discontinuities (maybe in a new plot altogether), and then perform a 'curve fitting' maneuver to elucidate an equation?
My pleasure!
I’m not certain how you would ‘connect’ the discontinuities other than by drawing a line between them. The three functions are discontinuous.
A linear regression is relatively easy to calculate:
x = -10:10;
y = x;
XY = [ones(size(x,2),1) x', y']; % Independent Variable Array
Z = [F1(1:end-1) F2(1:end-1) F3]'; % Dependent Variable Vector
B = XY\Z; % Calculate Parameters
F = XY*B; % Calculate Regression Line
then add this line between the ‘hold on’ and ‘hold off’ lines in the plot:
plot3(x,y,F, '--k', 'LineWidth',1)
The ‘Z’ vector concatenates the values of F1...F3 in a single vector to match the lengths of x and y. The regression treats all the x, y, and Z calculated points as a single set of values, and uses that set of data to calculate the parameters for the fit. The line that calculates ‘B’ will throw a warning that the matrix is rank deficient because both x and y are linearly dependent. It doesn’t cause problems except for the mldivide solver, that issues its warning and then gives an appropriate solution anyway.
This is probably the best you can realistically hope for. You might be able to fit a multivariate polynomial to it, but considering that will require you to experiment to choose an appropriate polynomial degree as well, will likely require more effort than any benefit it could provide.
That is brilliant. I'll try that and report back to you. Thanks!
EDIT: I am trying new things, but the're not quite working right - could you help me with these?
I am trying to make it so that a 'difference in formula' only occurs at a certain y, and not the x?
and also, I am now trying to make these into a surface. I always try to start simple and then add complexities. However, I'm getting a bit stuck.
Here's my attempt for the above, but it clearly doesn't work:
x = [0:10];
y = {0:3; 3:5; 5:10};
Formula1 = @(x,y) x + y;
Formula2 = @(x,y) 2.*x + 2.*y;
Formula3 = @(x,y) 3.*x + 2.*y;
F1 = Formula1(x{1},y{1});
F2 = Formula2(x{2},y{2});
F3 = Formula3(x{3},y{3});
c = colormap(jet(3));
figure(1)
surf(x{1},y{1},F1, 'Color',c(1,:))
hold on
surf(x{2},y{2},F2, 'Color',c(2,:))
surf(x{3},y{3},F3, 'Color',c(3,:))
hold off
grid on
xlabel('X')
ylabel('Y')
zlabel('F(X,Y)')
Star Strider
on 12 Oct 2014
Edited: Star Strider
on 12 Oct 2014
Thank you! My pleasure!
*EDIT — Unfortunately, because of the discontinuities, it is going to be difficult if not impossible to generate your data as a surface. The easiest way would be to use scatteredInterpolant, but it balks because of the collinearity of x and y.
I’ll post my code for you to experiment with:
x = {-10:-3; -3:5; 5:10};
y = x;
Formula1 = @(x,y) x + y;
Formula2 = @(x,y) 2.*x + 2.*y;
Formula3 = @(x,y) 3.*x + 2.*y;
[X1,Y1] = meshgrid(-10:-3);
[X2,Y2] = meshgrid(-3:5);
Z2 = Formula2(X2,Y2);
[X3,Y3] = meshgrid(5:10);
Z3 = Formula2(X3,Y3);
figure(1)
mesh(X1,Y1,Z1)
hold on
mesh(X2,Y2,Z2)
mesh(X3,Y3,Z3)
hold off
grid on
xlabel('X')
ylabel('Y')
zlabel('F(X,Y)')
That plot looks strange, but is about as good as it’s going to get.
If you want to experiment with scatteredInterpolant, this will get you started:
F1 = Formula1(x{1},y{1});
[X1,Y1] = meshgrid(-10:-3);
F1i = scatteredInterpolant(x{1}', y{1}', F1','natural');
Z1 = F1i(X1,Y1);
The documentation has a number of examples. You can also experiment with columns of random numbers to see how it works, and why it won’t work with collinear data.
A
on 12 Oct 2014
Hi Star! I made additions to my previous message, not sure if you've seen it. Is it okay I ask these questions here or should I make a new thread?
I'm sorry if I'm asking too much.
A
on 14 Oct 2014
Dear Star,
Played around with it a bit and I think I'm on to something here. I think I have the basic shape that I would be happy with. My next steps are to 'color' them each differently and then further down the pipeline I want to be able to somehow 'connect them' and yield a 'formula' out of the three shapes:
x = [0:10]
y = {0:3; 3:5; 5:10};
Formula1 = @(x,y) x + y;
Formula2 = @(x,y) 2.*x + 2.*y;
Formula3 = @(x,y) 3.*x + 2.*y;
[X1,Y1] = meshgrid(x,0:3);
[X2,Y2] = meshgrid(x,3:5);
[X3,Y3] = meshgrid(x,5:10);
Z1 = Formula1(X1,Y1);
Z2 = Formula2(X2,Y2);
Z3 = Formula3(X3,Y3);
figure(1)
mesh(X1,Y1,Z1)
hold on
mesh(X2,Y2,Z2)
mesh(X3,Y3,Z3)
hold off
grid on
xlabel('X')
ylabel('Y')
zlabel('F(X,Y)')
Any thoughts/advice?
Cheers
Star Strider
on 14 Oct 2014
The only way I could think of to connect them would be to use scatteredInterpolant. That might work now that you’ve redefined the ranges of x and y. Adding small amounts of random noise to x and y would likely avoid the collinearity problem if that again arises. I’ll work on that later.
Since we’re now dealing with surfaces — that could be considered a new problem — and since you Accepted my Answer (Thank You!), making a new thread might attract others’ attention. That’s your call. For instance, I don’t contribute to threads with Accepted Answers because I figure there’s no point, since the issue has been resolved. I continue following my own Accepted Answers as long as I can contribute something useful. If they stray into areas beyond my expertise or become unmanageably complicated, I politely decline to participate further.
A
on 14 Oct 2014
You've been very helpful on this and other occassions. Thank you!
My pleasure!
I had errands to run earlier today so was away. I experimented with scatteredInterpolant and came up with this code:
x = linspace(0,10,25)+rand(1,25)*0.1;
y = linspace(0,10,25)+rand(1,25)*0.1;
Formula1 = @(x,y) x(x>=0 & x<=3) + y(y>=0 & y<=3);
Formula2 = @(x,y) 2.*x(x>=3 & x<=5) + 2.*y(y>=3 & y<=5);
Formula3 = @(x,y) 3.*x(x>=5 & x<=10) + 2.*y(y>=5 & y<=10);
z1 = Formula1(x,y);
z2 = Formula2(x,y);
z3 = Formula3(x,y);
zv = [z1 z2 z3];
zs = scatteredInterpolant(x(1:length(zv))', y(1:length(zv))', zv', 'natural');
[X, Y] = meshgrid(x,y);
Z = zs(X,Y);
figure(1)
surfc(X, Y, Z)
grid on
It runs (most of the time, since the rand calls to avoid collinearity occasionally glitch with the inequalities). It’s also not consistent, likely for the same reason. I converted the plot call to surfc to make the irregularities more apparent.
Experiment with it. This is an interesting challenge!
More Answers (0)
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)