Plotting Multiple Functions on the same graph

14 Ansichten (letzte 30 Tage)
Will Jeter
Will Jeter am 4 Mär. 2021
Beantwortet: KSSV am 4 Mär. 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

Akzeptierte Antwort

KSSV
KSSV am 4 Mär. 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

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by