Plotting a piecewise Function?

조회 수: 5 (최근 30일)
mk_ballav
mk_ballav 2014년 11월 16일
답변: Star Strider 2014년 11월 16일
Hey guys. I need a plot of a piecewise function in MATLAB and I don't know how to do it. I have alpha defined as, alpha = 0:0.1:2. here n1, n2 are numbers I defined earlier and vj(1,1), vj(2,1) are matrix elements.
f(x) = v1(1,1)*exp(1i*alpha*x)+v1(2,1)*exp(-1i*alpha*x), 0<x<1;
v2(1,1)*exp(1i*alpha*(x-1))+v2(2,1)*exp(-1i*alpha*(x-1)), 1<x<1+n1/n2;
v3(1,1)*exp(1i*alpha*(x-1-n1/n2))+v3(2,1)*exp(-1i*alpha*(x-1-n1/n2)), 1+n1/n2<x<2+n1/n2;
v4(1,1)*exp(1i*alpha*(x-2-n1/n2))+v4(2,1)*exp(-1i*alpha*(x-2-n1/n2)), 2+n1/n2<x<2+2n1/n2;
v5(1,1)*exp(1i*alpha*(x-2-2n1/n2))+v5(2,1)*exp(-1i*alpha*(x-2-2n1/n2)), 2+2n1/n2<x<3+2n1/n2;
I want to plot abs(f) versus x.

채택된 답변

Star Strider
Star Strider 2014년 11월 16일
This works:
[v1,v2,v3,v4 v5] = deal(rand(2,1)); % Created Data
n1 = 3; % Created Data
n2 = 5; % Created Data
x = linspace(-pi,pi,25); % Created Data
alpha = 0:0.1:2;
f = @(x,alpha) [v1(1,1).*exp(1i.*alpha.*x)+v1(2,1).*exp(-1i.*alpha.*x).*(0<x & x<1) + ...
v2(1,1).*exp(1i.*alpha.*(x-1))+v2(2,1).*exp(-1i.*alpha.*(x-1)).*(1<=x & x<(1+n1/n2)) + ...
v3(1,1).*exp(1i.*alpha.*(x-1-n1/n2))+v3(2,1).*exp(-1i.*alpha.*(x-1-n1/n2)).*(1+n1/n2<=x & x<(2+n1/n2)) + ...
v4(1,1).*exp(1i.*alpha.*(x-2-n1/n2))+v4(2,1).*exp(-1i.*alpha.*(x-2-n1/n2)).*(2+n1/n2<=x & x<(2+2.*n1/n2)) + ...
v5(1,1).*exp(1i.*alpha.*(x-2-2.*n1/n2))+v5(2,1).*exp(-1i.*alpha.*(x-2-2.*n1/n2)).*(2+2.*n1/n2<=x & x<(3+2.*n1/n2))];
[X,A] = meshgrid(x,alpha);
F = abs(f(X,A));
figure(1)
surf(X,A,F)
grid on
xlabel('\itx\rm')
ylabel('\alpha')
zlabel('|\itf\rm|')
Obviously, you will use your own data. I don’t have them so I created data to test it. Also, there were operators missing that I assumed were multiplications, so I inserted them.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by