Plotting a piecewise function
4 ビュー (過去 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
2014 年 11 月 9 日
This quickly became so convoluted and ambiguous that I deleted my answer.
採用された回答
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
2014 年 11 月 10 日
I noticed that I left out
v{1}*
in the second term, so I edited to fix that.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Subplots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!