# Help with Function of Do or Nested For Loop [closed]

I'm trying to define a function that calculates a variable 'prob'. Here's the function in Octave:

a=[1/sqrt(3),1/sqrt(3),1/sqrt(3)];

theta=[0,0,0];
gammma=[180,180,180];

prob=0;

for i=1:3
prob+=a(i)^2*cos(theta(i))*cos(gammma(i));
endfor

for i=1:3
for j=1:3
prob+=a(i)*a(j)*(cos(theta(i))*cos(gammma(j))+cos(theta(j))*cos(gammma(i)));
endfor
endfor


I've tried to recreate this code in Mathematica by doing the following:

f=For[i=1,i=3,i=t+1,For[j=1,j=3,j=j+1,prob=prob+a[[i]]^2*Cos[theta[[i]]]*Cos[gammma[[i]]]+a[[i]]*a[[j]]*(Cos[theta[[i]]]*Cos[gammma[[j]]]+Cos[theta[[j]]]*Cos[gammma[[i]]])]]


and

f=Do[prob=prob+a[[i]]^2*Cos[theta[[i]]]*Cos[gammma[[i]]]+a[[i]]*a[[j]]*(Cos[theta[[i]]]*Cos[gammma[[j]]]+Cos[theta[[j]]]*Cos[gammma[[i]]]),{i,3},{j,3}]


But neither of these statements is running. There's no error or anything showing up. It just doesn't run when I press shift enter, though the red rexecuting icon pops up for a second so I know SOMETHING is happening!

Also, once I get this down, I want f to be a function so I can calculate ranges for a, theta and gamma for when f<0. Vectors a, theta and gammma are arbitrarily defined right now, though I want to eventually use Mathematica's linguistic variable capability to find this.

That is, something like Reduce[f<0,{a,0,1},{theta,0,180},{gammma,0,180}]. I'm not sure how to define functions in Mathematica to be used in this manner, I've tried looking at different documentations.

Please help, I'm utterly confounded. Thanks in advance!

## closed as off-topic by Szabolcs, Simon Rochester, happy fish, Feyre, SaschaJan 10 '17 at 14:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Szabolcs, Simon Rochester, happy fish, Feyre, Sascha
If this question can be reworded to fit the rules in the help center, please edit the question.

• The code is running, but Do and For do not return anything. It makes no sense to evaluate f = Do[...] as Do always returns Null. It is not clear from your question what you expected to happen. Did you check the value of prob? – Szabolcs Jan 10 '17 at 10:19
• But Matlab does not use endFor at all. Also Matlab does not support +=. Are you sure the code is written in Matlab? – Nasser Jan 10 '17 at 10:19
• @ Nasser Sorry I meant to write Octave. Not Matlab. :) @Szabolcs Thanks, that makes sense that Do doesn't return anything, I hadn't thought of that. I checked the value of prob afterwards, it does return something plausible. How can I define a function that uses Do, then? If I want to use Reduce the way I've written in my question? – user3625380 Jan 10 '17 at 10:29
• Look up Module and see the examples. It will show you how to return any value... – Szabolcs Jan 10 '17 at 10:40

## 1 Answer

This is direct translation. But one can make this more functional if needed. ps Watch for your angles. They should be in Radians. So use Degree in M. You Matlab code was wrong. And avoid For. Use Table or Do if needed.

a = {1/Sqrt, 1/Sqrt, 1/Sqrt};
theta = {0, 0, 0};
gammma = {180 Degree, 180 Degree, 180 Degree};

prob1 = Total[Table[a[[i]]^2 Cos[theta[[i]]] Cos[gammma[[i]]], {i, Length@a}]];

prob2 = Table[a[[i]] a[[j]] (Cos[theta[[i]]] Cos[gammma[[j]]] +
Cos[theta[[j]]] Cos[gammma[[i]]]), {i, Length@a}, {j, Length@a}];

prob = prob1 + Total[prob2, 2] Matlab:

a=[1/sqrt(3),1/sqrt(3),1/sqrt(3)];
theta=[0,0,0];
gammma=[180,180,180]*pi/180;

prob=0;

for i=1:3
prob= prob+ (a(i)^2*cos(theta(i))*cos(gammma(i)));
end

for i=1:3
for j=1:3
prob= prob + (a(i)*a(j)*
(cos(theta(i))*cos(gammma(j))+cos(theta(j))*cos(gammma(i))));
end
end
prob

-7.0000

• wow this is so clean and concise! Thanks for bringing the Table function to my notice, I'm completely new to Mathematica. And I'm sorry, I meant to write Octave, not Matlab. Thank you so much. Could you please tell me how I can go about defining a function for which I can perform Reduce, using prob 1 and prob 2? I imagine it would be a multivariable function f[a1_,a2_,a3_,theta1_,theta2_, etc.]. I want to find the values of the three vectors where prob<0. – user3625380 Jan 10 '17 at 10:46
• @user3625380 Please make new question on that, and make it clear what the the problem is. As I do not understand now what you are asking now. Better to keep each question limited in scope to make it easier to answer. I answered your question about the translation only now. – Nasser Jan 10 '17 at 10:47
• is there a way for me to private message you? I don't want to spam stack ex with another naive question. – user3625380 Jan 10 '17 at 10:50
• @user3625380 you can use chat to talk to some one. Use the chat button at the top of the main page. – Nasser Jan 10 '17 at 10:53
• @user3625380 Note that the more functional approach could be just prob = Total[a^2 Cos[theta] Cos[gammma]] + Total[2 KroneckerProduct[a Cos[theta], a Cos[gammma]], 2] – Simon Rochester Jan 10 '17 at 11:18