MATLAB: I have encounter an error with the following: Error in ShortCircu​itCurrentI​ncrease (line 48) RadiantIntensity(i) = integral2(polarfun{: , 181 },PolarAng​le3(i),0,p​i,0,pi); Anyone Help please

0181 }error in shortcircuitcurrentincrease (line 48) radiantintensity(i) = integral2(polarfun{:pipi); anyone help pleasepolarangle3(i)xyz

PolarAngle2 = [-pi/2:(1/180*pi):pi/2]; %incremental of polar angle of sun rays
PolarAngle3 = PolarAngle2';
y = sin(PolarAngle3);
for (i=1:1:181)
NormalizedIntensity1(i) = NormalizedIntensity(i) * y(i);
RadiantIntensity = zeros(size(NormalizedIntensity1));
RadiantIntensity = RadiantIntensity';
polarfun = @(AzimuthAngle,PolarAngle3) 1./((NormalizedIntensity1(i))');
end
for(i=1:1:181)
RadiantIntensity(i) = integral2(polarfun{: , 181 },PolarAngle3(i),0,pi,0,pi);
figure;
plot(Wavelengthnm,RadiantIntensity);
xlabel('Wavelength(nm)');
ylabel('WavelengthDependentIrradiance');
title('WavelengthDependentIrradiance against Wavelength');
end

Best Answer

Ummm, your code appears to not be very suitable for any purpose.
In your code
for (i=1:1:181)
NormalizedIntensity1(i) = NormalizedIntensity(i) * y(i);
RadiantIntensity = zeros(size(NormalizedIntensity1));
RadiantIntensity = RadiantIntensity';
polarfun = @(AzimuthAngle,PolarAngle3) 1./((NormalizedIntensity1(i))');
end
you keep reassigning RadiantIntensity but do not use it in that loop. The only reason to keep reassigning it seems to be that your NormalizedIntensity1 is potentially getting larger each time, and it appears that you want your RadiantIntensity to match the final outcome. But you could do that without reassigning it in the loop: you could just assign it once at the end of the loop.
Your NormalizedIntensity1(i) could also be calculated in vectorized form without using a loop.
NormalizedIntensity1 = NormalizedIntensity(:) .* y(:);
RadiantIntensity = zeros( size(NormalizedIntensity1) );
Inside that first loop you are continually writing over the function handle polarfun. But then in the second loop you appear to be attempting to access it using function handle notation. Perhaps you want your loop to be
for i = 1 : length(NormalizedIntensity1)
polarfun{i} = @(AzimuthAngle,PolarAngle3) 1./NormalizedIntensity1(i);
end
This would create a cell array of function handles, a different one for each NormalizedIntensity1 value. Each of the handles would, however, instruct that the angles be ignored and the constant 1./NormalizedIntensity1(i) be returned . If that is what you want then there would be much better ways of using that in an integral.
Your second loop has
for(i=1:1:181)
RadiantIntensity(i) = integral2(polarfun{: , 181 },PolarAngle3(i),0,pi,0,pi);
figure;
plot(Wavelengthnm,RadiantIntensity);
xlabel('Wavelength(nm)');
ylabel('WavelengthDependentIrradiance');
title('WavelengthDependentIrradiance against Wavelength');
end
You initialized RadiantIntensity in your previous loop so it is already at maximum size, so you are effectively filling in values from the beginning and making a different plot each time you do that. Those different plots are just going to add one more non-zero value each and it is not apparent why one would want to do that.
If you did want a different polarfun for each i, then it would not make sense to always ask for the 181'st column of them -- if you were going to do that then it would not make sense to build anything except the last of them, like
polarfun = @(AzimuthAngle,PolarAngle3) 1./NormalizedIntensity1(181);
and
RadiantIntensity(i) = integral2(polarfun,PolarAngle3(i),0,pi,0,pi); %but see later

and if you do want a different one for each i then
RadiantIntensity(i) = integral2(polarfun{i},PolarAngle3(i),0,pi,0,pi); %but see later
However a problem with either version is that integral2 takes 5 inputs, not 6 inputs. The first input is the function handle; the second is the lower bound on the first variable, the third is the upper bound on the first variable, the fourth is the lower bound on the second variable and the fifth is the upper bound on the second variable. In what you call, the 0, pi appear to be lower bound and upper bound pairs, so the reference to PolarAngle3(i) appears to be out of place and should probably be omitted. That would make your calling sequence
RadiantIntensity(i) = integral2(polarfun{i},0,pi,0,pi);
if you created a different polarfun for each i, or
RadiantIntensity(i) = integral2(polarfun,0,pi,0,pi);
if you want to use the same polarfun for all of them. You might notice, though, that this is always going to be the same value for each i, so your result would be a constant.
If your integral is of the form
@(AzimuthAngle,PolarAngle3) 1./NormalizedIntensity1(i);
which ignores the two input angles, then integral2() over 0..pi, 0..pi of that is going to be
1./NormalizedIntensity1(i) * (pi-0)*(pi-0);
which is pi^2./ NormalizedIntensity1(i) which hardly seems worth calculating as an integral.
I do not know what you think you are calculating, but at the moment it seems plausible that it could all be taken down into a small number of vectorized lines.