Plotting a composite symbolic function using ezplot

조회 수: 10 (최근 30일)
Christian Walther Andersen
Christian Walther Andersen 2012년 10월 7일
I want two plot to functions in the same plot but on two different intervals. E.g.
f(x) = { 2*x , if 0<x<2 } { cos(x), if 2<x<pi }
I want to be able to plot both of these parts of f on their respective interval. I've tried "hold on", but that doesn't work, it only displays the last part of the function given. When I put hold on and tell it to draw both functions on the same interval, it does that. I would like to have it split up though.
Thanks in advance
Christian Walther Andersen

답변 (2개)

Star Strider
Star Strider 2012년 10월 7일
My suggestion:
y = @(x) [2*x(x <= 2) sin(x( (x >= 2) & (x <= pi) ) )];
figure(1)
ezplot(y, [0 pi])

Christian Walther Andersen
Christian Walther Andersen 2012년 10월 9일
I guess I could have explained my self better, but in my case, the actual functions and input were function-inputs, so they could differ. I appreciate your answer, though I found a way to wrok around it:
By converting the symbolic expression to a matlab-function (function handle), I just plot normally:
g = matlabFunction(Func1);
x = linspace(0,2,100);
plot(x,g(x));
and then the same for func2. That worked for me
  댓글 수: 1
Star Strider
Star Strider 2012년 10월 9일
편집: Star Strider 2012년 10월 9일
That was my first approach, but since you specified ezplot, I used it instead.
My approach (creating a vector by concatanating the values returned by the various functions with respect to different ranges of the values of the x vector) would work for function inputs as well (as it did with the sin function), since the logical indexing of the arguments that I used is independent of the function. You could probably use symbolic functions, but since those only became available with 2012a and I do not know what version you have, I opted not to use them.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by