Is there a way I can create one graph out of many other graphs? (i.e., a combo graph)
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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
채택된 답변
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
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
2014년 10월 12일
편집: Star Strider
2014년 10월 12일
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.
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.
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
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.
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!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
