Creating one curve from different curves
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Rik
le 24 Juin 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)
0 commentaires
Voir également
Catégories
En savoir plus sur Fit Postprocessing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!