Plotting a piecewise curve

3 ビュー (過去 30 日間)
Ben Phelps
Ben Phelps 2020 年 11 月 25 日
回答済み: Sulaymon Eshkabilov 2020 年 11 月 27 日
I am trying to plot the following curve
I have written the following:
n=...;
tau=...;
t0=0;
tend=...;
t = linspace(t0,tend);
x = zeros(length(t));
for i=1:n
for j=1:length(t)
if ((i-1)*tau <= t(j)) && (t(j) <= i*tau)
A = zeros(i);
for k=1:i
A(k) = (-1)^k*((t(j)-(k-1)*tau)^k)/factorial(k);
end
x(j) = 1 + sum(A);
end
end
end
plot(t,x)
Of course with the values of n, tau and tend filled in depending on what I need it to be. This is where the problem arrises though as the code does not work for any value of n>1.
Any help would be greatly appreciated

回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020 年 11 月 27 日
% Vectorization is much more efficient and thus, it is better to use it here:
n=100; tau =0.2; t = (n-1)*tau:tau/n:n*tau;
Useries = 1+ (t(1:end-1)-((1:n) -1)*tau).^(1:n)./factorial(1:n);
plot(t(1:end-1), Useries, 'b-o'), grid on, shg

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by