Creating one curve from different curves

조회 수: 2 (최근 30일)
Ziv Abramovic
Ziv Abramovic 2020년 6월 24일
답변: Rik 2020년 6월 24일
I would like to merge three curves to only one. the first curve is a sector of an ellipse from pi to 4/3*pi. the second curve is upper half circle from 4/3*pi to 5/3*pi, and the third is again a sector of an ellipse from 5/4*pi to 2pi.
This is sketch:
  댓글 수: 2
Rik
Rik 2020년 6월 24일
Do you mean just as lines, or do you want a piecewise function?
Ziv Abramovic
Ziv Abramovic 2020년 6월 24일
a piecewise function

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

답변 (1개)

Rik
Rik 2020년 6월 24일
There are at least 3 methods I could think of, each with their own pros and cons:
figure(1),clf(1)
%% with the symbolic toolbox
syms x
cond1= x<-2;
val1= -x;
cond2= x>=-2 & x<5;
val2= x.^2-2;
cond3= x>=5;
val3= 23+sin((x-5)*3);
f=piecewise(cond1,val1,cond2,val2,cond3,val3);
figure(1)
ax=subplot(1,3,1);cla(ax);
fplot(f,[-10 10])
%% with an anonymous function
%(note that this doesn't work if a val is NaN, even if the cond is false)
cond1=@(x) x<-2;
val1=@(x) -x;
cond2=@(x) x>=-2 & x<5;
val2=@(x) x.^2-2;
cond3=@(x) x>=5;
val3=@(x) 23+sin((x-5)*3);
f=@(x) 0 + ...
cond1(x).*val1(x) + ...
cond2(x).*val2(x) + ...
cond3(x).*val3(x);
figure(1)
ax=subplot(1,3,2);cla(ax);
fplot(f,[-10 10])
%% with array operations
%(also works if a val is NaN when the cond is false)
x=linspace(-10,10,200);
y=zeros(size(x));
cond=x<-2;
val= -x;
y(cond)=val(cond); % or if val is still an anonymous function: y(cond)=val(x(cond));
cond= x>=-2 & x<5;
val= x.^2-2;
y(cond)=val(cond);
cond= x>=5;
val= 23+sin((x-5)*3);
y(cond)=val(cond);
figure(1)
ax=subplot(1,3,3);cla(ax);
plot(x,y)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by