Plotting a piecewise function

2 views (last 30 days)
mk_ballav
mk_ballav on 9 Nov 2014
Commented: the cyclist on 10 Nov 2014
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 Comment
Star Strider
Star Strider on 9 Nov 2014
This quickly became so convoluted and ambiguous that I deleted my answer.

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 9 Nov 2014
Edited: the cyclist on 10 Nov 2014
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 Comment
the cyclist
the cyclist on 10 Nov 2014
I noticed that I left out
v{1}*
in the second term, so I edited to fix that.

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!