MATLAB: Matrix dimension must agree

matrix

Hi,
I need some help with troubleshooting this code. It keeps giving me the error
Error in Assigment2problem1function (line 16)
It11=I0f.*10.^(-A1);
Error using .*
Matrix dimensions must agree.
function [ Absorbance1,Absorbance2 ] = Assigment2problem1function(I0,E1,E2,l,Nrep)
%I0=100000, E1=10000, E2=20000, l=1, Nrep=3
% Detailed explanation goes here
% A = ECl beer-lambert law
% A = -log T = -log(It/Io)=log(I0)-log(It)
% A = Absorbance
%It = Intensity of Transmitted photons
%I0 = Initial Intensity of photons
% Assigment2problem1function(100000,10000,20000,1,3)
C=10:10:50*(10^-6);
SD=sqrt(I0);
for i=1:Nrep
I0f=I0+rand(1,Nrep)*SD;
A1=E1*C*l;
It11=I0f.*10.^(-A1);
SD21=sqrt(It11);
It12=It11+SD21.*rand(1,1);
Absorbance1=log(I0)-log(It12)
A2=E2*C*l;
It21=I0f.*10.^(-A2);
SD21=sqrt(It21);
It22=It21+SD21.*rand(1,1);
Absorbance2=log(I0)-log(It22)
end

Best Answer

The problem is the way you define ‘C’. MATLAB interprets this as going from 10 to 50E-6 in steps of 10. It never iterates at all, because 50E-6 is less that the first iteration step of 10, so it produces an empty value. Even when you correct that by multiplying the entire vector (not only the last value) by 1E-6, you then get another dimension disagreement because with the ‘Nrep’ value you posted creates a vector length of 3 while ‘C’ has a length of 5.
The easiest way to solve these problems is to define ‘C’ as:
C = linspace(10, 50, Nrep)*1E-6;
Your function then runs without error. I leave it to you to determine that it produces the correct outputs.