# Plotting a piecewise function

18 ビュー (過去 30 日間)
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 2014 年 11 月 9 日
This quickly became so convoluted and ambiguous that I deleted my answer.

サインイン to comment.

### 採用された回答

the cyclist 2014 年 11 月 9 日

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 2014 年 11 月 10 日
I noticed that I left out
v{1}*
in the second term, so I edited to fix that.

サインイン to comment.

### その他の回答 (0 件)

サインイン してこの質問に回答します。

タグが未入力です。

Translated by