Plotting Multiple Functions on the same graph

15 vues (au cours des 30 derniers jours)
Will Jeter
Will Jeter le 4 Mar 2021
Réponse apportée : KSSV le 4 Mar 2021
Trying to plot this fourier sin series with n=1,n=5 and n=15 all on the same graph. Im not sure how. I tried using the hold on function but that didnt work. Here is what i have:
z = 0:0.001:1;
n = 1;
ysin = fsin(z,n);
plot(z,ysin),grid
xlabel('z'),ylabel('sin function')
% Define functions
function f = fsin(z,n)
f = zeros(1,numel(z));
for i = 1:n
an = (-6/(i*pi))*(cos(i*pi)-cos((i*pi)/2));
f = an*sin(n*pi*z)
end
end

Réponse acceptée

KSSV
KSSV le 4 Mar 2021
z = 0:0.001:1;
figure
hold on
for n = 1:5:15
ysin = fsin(z,n);
plot(z,ysin)
end
grid
xlabel('z'),ylabel('sin function')
legend('n=1','n=5','n=15')
% Define functions
function f = fsin(z,n)
f = zeros(1,numel(z));
for i = 1:n
an = (-6/(i*pi))*(cos(i*pi)-cos((i*pi)/2));
f = an*sin(n*pi*z) ;
end
end

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by