How to cycle through a 3x1 matrix

Hey all,
I'm working on a simple program and do not have much MATLAB experience. Attached is my code where all of the variables in the Br and Btheta are constant except for t. I am trying to figure out how to cycle through matrix t and do separate calculations. What keeps happening is it calculates the first Br and Btheta values and then stores those answers and uses them for the following two calculations how might I go about this? I tried a number of for loops, but like I said, I have very little experience.
Thanks!
uo = (4 * pi * 10.^-7);
m = (7.94 * 10.^22);
r = (6.371 * 10.^6);
t = [ 0 ; 90 ; 49.2392 ];
Br = [(-2 * uo * m)/(4 * pi * r.^3)] * cos(t);
Btheta = [(-uo * m)/(4 * pi * r.^3)] * sin(t);

 Accepted Answer

uo = (4 * pi * 10.^-7);
m = (7.94 * 10.^22);
r = (6.371 * 10.^6);
t = [ 0 ; 90 ; 49.2392 ];
num_t = length(t);
Br = zeros(num_t, 1);
Btheta = zeros(num_t, 1);
for t_idx = 1 : num_t
Br(t_idx) = [(-2 * uo * m)/(4 * pi * r.^3)] * cos(t(t_idx));
Btheta(t_idx) = [(-uo * m)/(4 * pi * r.^3)] * sin(t(t_idx));
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!