![]() |
使用MATLAB计算for循环内的许多正弦曲线,然后绘制它们
我在MATLAB中执行以下操作,并且效果很好。但是我需要计算20个正弦曲线而不是3个,然后将它们全部绘制出来。
x=sin(1*w*t)*(2/(pi*1)); y=sin(3*w*t)*(2/(pi*3)); z=sin(6*w*t)*(2/(pi*6)); plot(t,x,t,y,t,z) 我认为应该可以进行for循环然后作图,但是我不确定该如何完成并需要一些帮助。 [B]回答:[/B] [URL="http://www.mathworks.com/help/techdoc/ref/bsxfun.html"]BSXFUN[/URL]函数是解决您的问题的一种方法, [URL="https://stackoverflow.com/questions/3856379/with-matlab-calculate-a-number-of-sinusoids-inside-a-for-loop-and-then-plot-them/3856594#3856594"]如Amro所示[/URL] 。但是,如果您是MATLAB的新用户,那么更简单的for循环解决方案可能更易于理解,并且不那么令人生畏: w = 1; %# Choose the value of w k = 1:20; %# Your 20 values to compute a sinusoid for N = 100; %# The number of time points in each sinusoid t = linspace(0,2*pi,N).'; %'# A column vector with N values from 0 to 2*pi X = zeros(N,numel(k)); %# A matrix to store the sinusoids, one per column for iLoop = 1:numel(k) %# Loop over all the values in k X(:,iLoop) = sin(k(iLoop)*w*t)*(2/(pi*k(iLoop))); %# Compute the sinusoid %# and add it to X end plot(t,X); %# Plot all the sinusoids in one call to plot 以下是一些文档链接,这些链接可有助于全面了解上述解决方案的工作方式: [URL="http://www.mathworks.com/help/techdoc/ref/linspace.html"]LINSPACE[/URL] , [URL="http://www.mathworks.com/help/techdoc/ref/numel.html"]NUMEL[/URL] , [URL="http://www.mathworks.com/help/techdoc/ref/zeros.html"]ZEROS[/URL] , [URL="http://www.mathworks.com/help/techdoc/ref/plot.html"]PLOT[/URL] , [URL="http://www.mathworks.com/help/techdoc/ref/for.html"]For循环[/URL] , [URL="http://www.mathworks.com/help/techdoc/matlab_prog/f8-784135.html#f8-793781"]预分配数组以提高性能[/URL] 。 [url=https://stackoverflow.com/questions/3856379]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 23:21。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.