matlab 如何修复函数的输入参数包含冒号运算符错误

vfh0ocws  于 2023-05-18  发布在  Matlab
关注(0)|答案(1)|浏览(453)
clc; clear

w = load('dimensionless_concentration_data.txt');
time = w(:,1);
C_mea = w(:,2);

figure(1)
 plot(time,C_mea,'*','LineWidth',1.5,'Color','r')
 xlabel('Time (seconds)','FontName','Arial','FontSize',16);ylabel('Dimensionless Concentration','FontName','Arial','FontSize',16) 
 set(gca,'LineWidth',1.5,'FontSize',16,'FontName','Arial')
  
grid on
hold on 

c0 = 0.2; %(mol m^-3) initial concentration
cin = 2; %(mol m^-2) mass flux
x = 0.02; %mass flux

t = 0:200:3600; %time vector
    
D = 10^-7 %m^2s^-1 diffusivity coefficient upper bound
C =@(x,t) c0 +(cin./(sqrt(4*pi*D*t)).*exp(-x.^2./(4*D*t)));

for i =1:1000
    D = rand(1) * 10^-7; 
    D_vals(i) = D;
    D_est = mean(D_vals);
    STD_dev = std(C_mea);
    C_dim(i,:) = C(i,:)./max(C(i,:)); %dimensionless concentration
    R_fit(i) = 1/(STD_dev.^2).*(C_dim(i,2)./C_mea(1)-1).^2 + (C_dim(i,end)./C_mea(end)-1).^2;
    drawnow 
    fprintf('%d         %d      %d',i,R_fit(i),D_est)
end

此代码以以下错误结束:

Input arguments to function include colon operator. To input the colon character, use ':' instead.

Error in olasilikodev (line 31)
    C_dim(i,:) = C(i,:)./max(C(i,:)); %dimensionless concentration

我试着用这段代码来解决这个问题:

C_dim = zeros(1000, length(t));
R_fit = zeros(1000, 1);
D_vals = zeros(1000, 1);

但是没有用
我的数据:

% seconds    Diemnsonless concentration (C(1:length(t))/max(C))
300            0.02
500            0.16
1000           0.46
1250           0.65
1500           0.75
2200           0.88
2900           0.95
3200           0.97
3600           0.99
flmtquvp

flmtquvp1#

C = @(x,t) c0 +(cin./(sqrt(4*pi*D*t)).*exp(-x.^2./(4*D*t)));定义了一个函数,所以当你调用C(i,:)时,matlab会尝试将你的公式应用到x=it=:上。这将导致错误,因为:不是一个值。
我认为你应该继续将你的公式应用到变量上,将这一行替换为:C = c0 +(cin./(sqrt(4*pi*D*t)).*exp(-x.^2./(4*D*t)));
它会产生一个向量C,你可以计算C(i)./max(C)

相关问题