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

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

 Accepted 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.

15 Comments

Sorry Sir, I have a range of values for the NormalizedIntensity and also the corresponding variable attached to it for each individual data. And i am doing an double integral function for it. The reason why i am doing a double integral of pi is that i am doing half a circle which includes the azimuth angle and the polar angle.
I am still very confused too. and the error i had encountered is
ShortCircuitCurrentIncrease
Cell contents assignment to a non-cell array object.
Error in ShortCircuitCurrentIncrease (line 44)
polarfun{i} = @(AzimuthAngle,PolarAngle3)
1./NormalizedIntensity1(i);
Fixing only your syntax problems and not the problems that your code is not doing anything useful:
PolarAngle2 = [-pi/2:(1/180*pi):pi/2]; %incremental of polar angle of sun rays
PolarAngle3 = PolarAngle2';
y = sin(PolarAngle3);
clear polarfun
for (i=1:1:181)
NormalizedIntensity1(i) = NormalizedIntensity(i) * y(i);
RadiantIntensity = zeros(size(NormalizedIntensity1));
RadiantIntensity = RadiantIntensity';
polarfun{i} = @(AzimuthAngle,PolarAngle3) 1./((NormalizedIntensity1(i))');
end
for(i=1:1:181)
RadiantIntensity(i) = integral2(polarfun{i}, 0, pi, 0, pi);
figure;
plot(Wavelengthnm,RadiantIntensity);
xlabel('Wavelength(nm)');
ylabel('WavelengthDependentIrradiance');
title('WavelengthDependentIrradiance against Wavelength');
end
ok sir,
So the purpose of creating this code is because i want to do an double integration of 2 variables multiplied together which is the NormalizedIntensity and the sin(PolarAngle).
the boundary would be from 0-pi and 0-pi, or -pi/2 to pi/2.
any idea how to perform the integration in a easier way?
This is the following equation which i am trying to do. but i could not find a solution to the double integration to it.
To calculate that integral, you need Sn in formula form with two variables, which you do not show us: you start out with NormalizedIntensity in a 1D discrete form.
Your integral has Sn with theta and lambda as parameters, but you are integrating with respect to theta and phi, where phi has not been mentioned. Perhaps the expression of the integral is incorrect?
yes the equation is integrating with theta(polar angle) and phi(Azimuth Angle). but the normalized intensity is a function of wavelength and theta.
so the above codes i am using is to try to simulate the double integration to get the value of the output.
so for my codes above, does it not work?
Your code uses discrete NormalizedIntensity. It is not possible to use integral() or integrap2() on discrete data: you would need to use something like trapz()
If you have the formula for NormalizedIntensity we might be able to implement it in terms of integral2()
does trap has the same functionality as integral2?
PolarAngle2 = [-pi/2:(1/180*pi):pi/2]; %incremental of polar angle of sun rays
PolarAngle3 = PolarAngle2';
y = sin(PolarAngle3);
x = -pi/2:(1/180*pi):pi/2;
z = -pi/2:(1/180*pi):pi/2;
clear polarfun;
for i = 1 : length(NormalizedIntensity1)
SecondNormalizedIntensity(i) = NormalizedIntensity(i) * y(i);
RadiantIntensity = zeros(size(SecondNormalizedIntensity));
RadiantIntensity = RadiantIntensity';
polarfun = @(AzimuthAngle,PolarAngle3) 1./SecondNormalizedIntensity(i);
end
for(i=1:1:181)
RadiantIntensity{i} = trapz(z,trapz(x,polarfun,2));
figure;
plot(Wavelengthnm,RadiantIntensity);
xlabel('Wavelength(nm)');
ylabel('WavelengthDependentIrradiance');
title('WavelengthDependentIrradiance against Wavelength');
end
ShortCircuitCurrentIncrease
Undefined function 'permute' for input arguments of type 'function_handle'.
Error in trapz (line 38) y = permute(y,perm);
Error in ShortCircuitCurrentIncrease (line 69)
RadiantIntensity{i} = trapz(z,trapz(x,polarfun,2));
No, trapz does not have the same functionality as integral2(). You would not pass a function handle in to trapz. The second parameter to trapz() should be a data vector or data array.
I have been up all night so I am not going to work it all through at the moment.
okay sir, so but i thought trapz is an integration of using the trapezoid function?
trapz is numeric integration over an array of data, to be used when you have discrete data instead of a function.

Sign in to comment.

More Answers (0)

Asked:

Ang
on 30 Dec 2015

Commented:

Ang
on 5 Jan 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!