## How do is code this last summation? I was able to do the first one but have no clue how to finish it?

on 23 Mar 2019
Latest activity Commented on by Adam Schlei

on 23 Mar 2019

### darova (view profile)

function Senior_Project_code
%input values
y = 4;
n = 1;
L = 3;
%equations
%z=17;
a=L/(2*n);
x = 0:1:L;
E = 808/(y^2);
%c = atan(abs((x-a(2*i-1)))/y)*(180/3.14);
%s = (-0.0000000667*atan(abs((x-a(2*i-1)))/y)*(180/3.14).^4 ...
% + 0.0000157209*atan(abs((x-a(2*i-1)))/y)*(180/3.14).^3 ...
% - 0.0010081511*atan(abs((x-a(2*i-1)))/y)*(180/3.14).^2 ...
% + 0.0020817855*atan(abs((x-a(2*i-1)))/y)*(180/3.14) ...
% + 0.9991821678);
Axx(x+1) = 0;
for k = 1:n
Axx(x+1) = Axx(x+1) + E* ...
(-0.0000000667*(atan(abs((x-a*(2*k-1)))/y)*(180/3.14)).^4 ...
+ 0.0000157209*(atan(abs((x-a*(2*k-1)))/y)*(180/3.14)).^3 ...
- 0.0010081511*(atan(abs((x-a*(2*k-1)))/y)*(180/3.14)).^2 ...
+ 0.0020817855*(atan(abs((x-a*(2*k-1)))/y)*(180/3.14)) ...
+ 0.9991821678);
end
Ax = sum(Axx)/(L+1); %can I redo this to match my other equation?
display (Axx,'Brightness Values');
display (Ax,'Average Brightness');
end
Any help redoing Ax to match my above summation with i=1, to z-2... would be much appreciated.
Coding should be in for,if, then, and, or....type statements if possible.

Show 1 older comment
Walter Roberson

### Walter Roberson (view profile)

on 23 Mar 2019
That code could be made more efficient by not rcalculating the atan four times, and you could use atand() instead of 180/3.14 . And you could almost certainly get some vectorization there, process all of the k values at the same time and sum() those.
But it is not clear what your question is about redoing to match another equation?
The statement about x=0,1,..L is deceptive, seeming to imply that x will be integer valued -- something that you rely on. But if you look at what is being passed into s as the first parameter ("x") it is L/16 (okay because L is 16 so L/16 gives the integer 1), and L/32 (not okay as that would give 1/2) and L-L/32 (which would give 16-1/2=15.5, not an integer).

on 23 Mar 2019
Yes that is correct. I'm trying to code my MathCad formulas from the picture above. The problem I'm having is non-integer values. What I have in the picture is correct. The coding in Matlab is the problem I'm having. When I have non integer values I get array size errors. Is their ways around this, cause I originally had x = 0 : 0.1 : L so their were more test points being used, but the array sizes don't match then. And yes I do agree with all of what you saying (efficincy and rcalculating of atan four times), I'm not great at coding and any help for matching my MathCad program shown in the picture to the Matlab code would be great. Thanks a bunch.

on 23 Mar 2019
In other words I need the matlab equation of Ax, to be this summation equation with z=17:
or if not possible I need the matlab equation of Ax, to be this summation equation with z=17:
Any help would be great!!

### darova (view profile)

on 23 Mar 2019

Maybe a little bit clearer version:
y= 7.5, n=16, L=16 result: 118.22
y= 4, n=1, L=3 result: 46.31
function main
% input values
y = 4;
L = 3;
z = 17;
n = 1;
av = 0;
for i = 1:z-2
x = L/(z-1)*i;
av = av + s(x,y,n,L);
end
x = L/(2*z-2)*i;
av = av + s(x,y,n,L) + s(L-x,y,n,L);
av = av/z;
display('Average Brightness: '), av
end
function result = s(x,y,n,L)
% equations
E = 808.01/y^2.008;
a = L/(2*n);
c = @(i) atand( abs(x-a*[2*i-1])/y );
coeff = [
-0.0000000667
0.0000157209
-0.0010081511
0.0020817855
0.9991821678];
S = 0;
for i = 1:n
for j = 1:5
S = S + coeff(j)*c(i)^(5-j);
end
end
result = E*S;
end

on 23 Mar 2019
Thats awesome. Thanks.

on 23 Mar 2019
found the error. when x = L/(2*z-2)*i get rid of the i and it comes out prefect!

### Walter Roberson (view profile)

on 23 Mar 2019

%input values
y = 4;
n = 1;
L = 3;
z = 17;
%equations
c = @(x, y, n, l, i) atand(abs((x-l./(2.*n).*(2*i-1)))./y);
p = @(X) -0.0000000667*X.^4 + 0.0000157209*X.^3 - 0.0010081511*X.^2 + 0.0020817855*X + 0.9991821678;
s = @(x, y, n, l, c) sum(p(c(x(:), y, n, l, 1:n)),2);
zs = @(z, y, n, l, c) sum( s(l./(z-1).*(1:z-2), y, n, l, c), 1);
result = @(z, y, n, l, c) (zs(z, y, n, l, c) + s(l./(2*z-2), y, n, l, c) + s(l - l./(2*z-2), y, n, l, c)) ./ z;
result(z, y, n, L, c)
However the result is nowhere near 111

on 23 Mar 2019
When y= 7.5, n=16, and L=16 the Average or Ax equation should equal Ax=111.99
When y= 4, n=1, and L=3 the Average or Ax equation should equal Ax=44.94
not sure why you say L=3 then use a lower case l in your equation. Doesn't this screw things up? Also where does big X come from? That should be the tangent function or what c equals. Doesn't the use of (i) in the equation mean imaginary? This is why I change i to k. Not sure if that is a thing, but I'm not sure what your doing here with no explanation. It would be one thing if it worked, but Nice try anyways though.
Walter Roberson

### Walter Roberson (view profile)

on 23 Mar 2019
p = @(TEMPORARY_VARIABLE) -0.0000000667*TEMPORARY_VARIABLE.^4 + 0.0000157209*TEMPORARY_VARIABLE.^3 - 0.0010081511*TEMPORARY_VARIABLE.^2 + 0.0020817855*TEMPORARY_VARIABLE + 0.9991821678;
not sure why you say L=3 then use a lower case l in your equation. Doesn't this screw things up?
No. You need to learn about dummy parameters. For example
s = @(x, y, n, l, c) sum(p(c(x(:), y, n, l, 1:n)),2);
means the same thing as
s = @temporary_function_s;
function result = temporary_function_s(x, y, n, l, c)
p = evalin('caller', 'p');
result = sum(p(c(x(:), y, n, l, 1:n)),2);
end
which in turn means the same as
s = @temporary_function_s;
function result = temporary_function_s(First_Parameter, Second_Parameter, Third_Parameter, Fourth_Parameter, Fifth_Parameter)
p = evalin('caller', 'p');
result = sum(p(Fifth_Parameter(First_Parameter(:), Second_Parameter, Third_Parameter, Fourth_Parameter, 1:Third_Parameter)),2);
end
... almost. With some small details about exactly when p is imported into the function.