Plotting a piecewise function

2 次查看(过去 30 天)
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)))

更多回答(0 个)

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by