Plotting a piecewise curve

1 次查看(过去 30 天)
Ben Phelps
Ben Phelps 2020-11-25
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 CenterFile Exchange 中查找有关 Specifying 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