【发布时间】:2016-06-25 08:33:14
【问题描述】:
Matlab,我可以让 matlab 在 3 个单独的行上打印每个循环的答案吗? 我一直让它正确地绘制图形,但它似乎只打印最后一个 fprintf 还是一起打印 3 个总数? 提前致谢 P.S 很抱歉这个问题,因为我知道这是一个非常简单的问题,但我对 matlab 很陌生!
clc;
a= input('\n\nEnter a value for a: ');
b= input('\n\nEnter a value for b: ');
L= input('\n\nEnter a value for L: ');
a0 = (a+b)/2; %average value of the function over one complete cycle
t = 0:0.1:6*L;
fprintf('ft = %f',a0)
f=a0;
for k=1:2:20; %loop 10 times to give first 10 terms of the series
bk = ((b-a)*2)/(k*pi); %value for bk
f=f+bk*sin(k*pi*t/L);
fprintf(' + %fsin%ft',bk,(k*pi/L))
end
subplot(2,3,1);
plot(t,f,'g');
title('Fourier Series');
ylabel('Amplitude');
xlabel('Time');
grid on;
for k=1:2:50; %loop 25 times to give first 25 terms of the series
bk = ((b-a)*2)/(k*pi); %value for bk
f=f+bk*sin(k*pi*t/L);
%fprintf(' + %fsin%ft',bk,(k*pi/L))
end
subplot(2,3,2);
plot(t,f, 'r');
title('Fourier Series');
ylabel('Amplitude');
xlabel('Time');
grid on;
for k=1:2:100; %loop 50 times to give first 50 terms of the series
bk = ((b-a)*2)/(k*pi); %value for bk
f=f+bk*sin(k*pi*t/L);
%fprintf(' + %fsin%ft',bk,(k*pi/L))
end
subplot(2,3,4);
plot(t,f, 'b')
title('Fourier Series');
ylabel('Amplitude');
xlabel('Time');
grid on;
【问题讨论】: