【问题标题】:trying to create a subplot in MATLAB, does this code make sense?试图在 MATLAB 中创建子图,这段代码有意义吗?
【发布时间】:2021-09-05 23:14:59
【问题描述】:

我正在尝试在 matlab 中创建一个子图,但是此代码似乎无法使用。你对如何让它工作有什么建议吗? 我是一个编码新手,所以如果它不应该在这里,任何关于在哪里搜索这样的东西的帮助或建议将不胜感激。

 Uinput=inputdlg({'width:',...
                 'thickness:',...
                 'length:',...
                 'density:',...
                 'natural frequency 1:',...
                 'natural frequency 2:',...
                 'natural frequency 3:',...
                 'natural frequency 4:',...
                 'damping ratio:'},...
                 'input',1,{'','','','','','','','',''});
w=eval(Uinput{1});
t=eval(Uinput{2});
L=eval(Uinput{3});
rho=eval(Uinput{4});
f1=eval(Uinput{5});
f2=eval(Uinput{6});
f3=eval(Uinput{7});
f4=eval(Uinput{8});
zn=eval(Uinput{9});
mnum=eval(Uinput{6});
%%
%COMMENT FROM HERE TO END
fn=[2.3481 14.7152 41.2033 80.7406]; % natural frequencies taken from task 1
kn=((2*pi.*fn).^2)*(L*w*t*rho); % equation to calculate the 
zn1=0.01;
zn=[zn1 zn1 zn1 zn1];
phif=[0.011 0.063 0.159 0.294]; %force potential amplitudes and the applied position %mode shapes for the forcing location   %nodes and antinode locations  %YOU NEED TO CHANGE THESE. mode shapes for the forcing location for each mode
%50mm, 302mm, 600mm 
phir=[0.3464 0.7112 -0.0083 -0.7455]; % response potential %modeshapes for the response locations     %YOU NEED TO CHANGE THESE. mode shapes for the response location for each mode
f=linspace(0,100,5000);
for n = 1:length(fn),
    r = f/fn(n);
    hi(:,n) = phif(n)*phir(n)/kn(n)./(1-r.^2+j*2*zn(n)*r);
    phin=hi(:,n)/(max(abs(hi(:,n)));
    subplot(2,2,n);
    plot(x,phin, '-o')
    grid on
    xlabel('position')
    ylabel('normalised shape')
    title(['Mode ' num2str(mnum) ', ' num2str(fn) ' Hz'])
end
h=abs(sum(hi,2)); 
loglog(f,h)
ylabel('H')
xlabel('frequency (Hz)')

【问题讨论】:

    标签: matlab subplot


    【解决方案1】:

    第 34 行缺少括号。 第 36 行,“x”没有在任何地方定义。

    在解决了这 2 个错误之后,这段代码可以正常运行并绘图。

    【讨论】:

      【解决方案2】:

      #请试试这个修改后的代码! #Line 34 - 缺少括号 #第 36 行 - 代替 'x' 变量,请使用 'f' 变量然后绘图 #plot(f,phin, '-o')

       Uinput=inputdlg({'width:',...
                       'thickness:',...
                       'length:',...
                       'density:',...
                       'natural frequency 1:',...
                       'natural frequency 2:',...
                       'natural frequency 3:',...
                       'natural frequency 4:',...
                       'damping ratio:'},...
                       'input',1,{'','','','','','','','',''});
      w=eval(Uinput{1});
      t=eval(Uinput{2});
      L=eval(Uinput{3});
      rho=eval(Uinput{4});
      f1=eval(Uinput{5});
      f2=eval(Uinput{6});
      f3=eval(Uinput{7});
      f4=eval(Uinput{8});
      zn=eval(Uinput{9});
      mnum=eval(Uinput{6});
      %%
      %COMMENT FROM HERE TO END
      fn=[2.3481 14.7152 41.2033 80.7406]; % natural frequencies taken from task 1
      kn=((2*pi.*fn).^2)*(L*w*t*rho); % equation to calculate the 
      zn1=0.01;
      zn=[zn1 zn1 zn1 zn1];
      phif=[0.011 0.063 0.159 0.294]; %force potential amplitudes and the applied position %mode shapes for the forcing location   %nodes and antinode locations  %YOU NEED TO CHANGE THESE. mode shapes for the forcing location for each mode
      %50mm, 302mm, 600mm 
      phir=[0.3464 0.7112 -0.0083 -0.7455]; % response potential %modeshapes for the response locations     %YOU NEED TO CHANGE THESE. mode shapes for the response location for each mode
      f=linspace(0,100,5000);
      for n = 1:length(fn)
          r = f/fn(n);
          hi(:,n) = phif(n)*phir(n)/kn(n)./(1-r.^2+ j*2*zn(n)*r);
          phin=hi(:,n)/(max(abs(hi(:,n))))
          subplot(2,2,n);
          plot(f,phin, '-o')
          grid on
          xlabel('position')
          ylabel('normalised shape')
          title(['Mode ' num2str(mnum) ', ' num2str(fn) ' Hz'])
      end
      h=abs(sum(hi,2)); 
      loglog(f,h)
      ylabel('H')
      xlabel('frequency (Hz)')
      

      【讨论】:

        猜你喜欢
        • 2019-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        • 1970-01-01
        • 2020-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多