Plotting a piecewise function

조회 수: 2 (최근 30일)
mk_ballav
mk_ballav 2014년 11월 9일
댓글: the cyclist 2014년 11월 10일
Hey guys. I need a plot of a piecewise function in MATLAB and I don't know how to do it.
f(x) =
v1(1,1)*exp(1i*alpha*x)+v1(1,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<2;
v3(1,1)*exp(1i*alpha*(x-2))+v3(1,1)*exp(-1i*alpha*(x-2)), 2<x<3;
and upto (x-10) like this.
here vk(1,1)and vk(2,1) are matrix elements of 2*1 matrix.
How do I plot abs(f(x)) with alpha in MATLAB with alpha = 0:0.1:2?
  댓글 수: 1
Star Strider
Star Strider 2014년 11월 9일
This quickly became so convoluted and ambiguous that I deleted my answer.

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

채택된 답변

the cyclist
the cyclist 2014년 11월 9일
편집: the cyclist 2014년 11월 10일
It is a bit tricky. I think I got this right, but you should definitely check carefully.
x = 0 : 0.001 : 10;
% Define alpha
alpha = 0.3;
% Define some random constants corresponding to your v1...v10.
% Used cell array instead of awkward variables v1, etc.
for n = 1:10
v{n} = rand();
end
% Define the individual functions. Note that each will be zero over anything but its piecewise domain.
for n=1:10
f{n} = @(x) (x>(n-1) & x<=n) .* (v{1}*exp(1i*alpha*(x-n+1)) + v{1}*exp(-1i*alpha*(x-n+1)));
end
% Define the combined function
F = @(x) (f{1}(x) + f{2}(x) + f{3}(x) + f{4}(x) + f{5}(x) + f{6}(x) + f{7}(x) + f{8}(x) + f{9}(x) + f{10}(x));
% Plot the real and imaginary parts
figure
subplot(2,1,1), plot(x,real(F(x)))
subplot(2,1,2), plot(x,imag(F(x)))
  댓글 수: 1
the cyclist
the cyclist 2014년 11월 10일
I noticed that I left out
v{1}*
in the second term, so I edited to fix that.

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

추가 답변 (0개)

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by