Creating one curve from different curves

2 Ansichten (letzte 30 Tage)
Ziv Abramovic
Ziv Abramovic am 24 Jun. 2020
Beantwortet: Rik am 24 Jun. 2020
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 Kommentare
Rik
Rik am 24 Jun. 2020
Do you mean just as lines, or do you want a piecewise function?
Ziv Abramovic
Ziv Abramovic am 24 Jun. 2020
a piecewise function

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Rik
Rik am 24 Jun. 2020
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)

Kategorien

Mehr zu Fit Postprocessing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by