Plotting a piecewise curve

조회 수: 1 (최근 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 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