Plotting a piecewise curve

1 visualización (últimos 30 días)
Ben Phelps
Ben Phelps el 25 de Nov. de 2020
Respondida: Sulaymon Eshkabilov el 27 de Nov. de 2020
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

Respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 27 de Nov. de 2020
% 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

Categorías

Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by