Ask your own question, for FREE!
Mathematics 21 Online
OpenStudy (raffle_snaffle):

Matlab help!

OpenStudy (raffle_snaffle):

@phi

OpenStudy (raffle_snaffle):

How to find max_L and max_C in the power matrix. I need to use find command.

OpenStudy (raffle_snaffle):

Never mind got it figured out.

OpenStudy (raffle_snaffle):

% defining input variables for RLC circuit f_volt = 60; % measured in hertz W_volt = 120; % measured in volts rms R = 100; % measured in ohms L_o = 0.005:0.005:0.245; % matrix L 2D matrix incr. from left to right C_o = 5e-6:10e-6:9.5e-5; % matrix C 2D matrix incr. from top to bottom [NewL_o, NewC_o] = meshgrid(L_o, C_o); % current output for RLC circuit (I) in RLC circuit (note: j = sqrt(-1)) I = (120)./(100 + j*2*pi*60*NewL_o - ((j)./(2*pi*60*NewC_o))); % power obsorbed by resistor P_R = 100*abs(I.^2); % defining new variable max PR max_PR = max(max(P_R)); % find max L and C power [max_L, max_C] = find(P_R == max_PR); % 3D graph surf(P_R) xlabel('L'), ylabel('C'), title('Power (W)')

OpenStudy (raffle_snaffle):

Got it.

OpenStudy (phi):

First, I think you swapped the indices, and it should read [max_C_ix, max_L_ix]= find(P_R == max_PR); second, those are indices. the actual values are max_C= C_o(max_C_ix) or if you like: max_C= NewC_o(max_C_ix, max_L_ix); similarly max_L= L_o(max_L_ix)

OpenStudy (raffle_snaffle):

Okay. I actually compared answers with another student and we got the same values.

OpenStudy (phi):

what do you get for L and C ?

OpenStudy (phi):

max_L = 0.075000 max_C = 9.5000e-05 ?

OpenStudy (raffle_snaffle):

I didn't get those numbers..

OpenStudy (raffle_snaffle):

hold on @phi let me show you my ouput.

OpenStudy (raffle_snaffle):

Did I also tell you I had the wrong increments for the C_o

OpenStudy (raffle_snaffle):

i changed the indices

OpenStudy (phi):

your "max values" are the index into the array where the max value is. for example if you had A= [ 1 3 1] your "max" would be 2 (because A(2) is where the max value is) if you want the actual value, you have to use the original array C_o or L_o

OpenStudy (raffle_snaffle):

hold on

OpenStudy (raffle_snaffle):

I am reading what you posted above and recoding.

OpenStudy (raffle_snaffle):

[max_C, max_L] = find(P_R == max_PR) this doesn't read actual values just indices?

OpenStudy (phi):

to find match up the indices, notice P_R 49x10 has shape 49 x 10 the size 49 "goes with" L_o 1x49 so you should write [L_ix , C_ix ] = find(P_R == max_PR) L_ix = 15 C_ix = 10 the actual values are L_o(L_ix) ans = 0.075000 C_o(C_ix) ans = 9.5000e-05

OpenStudy (phi):

as a check, do these values give the max power? >> Lm= L_o(L_ix) Lm = 0.075000 >> Cm= C_o(C_ix) Cm = 9.5000e-05 >> II=(120)./(100 + j*2*pi*60*Lm - ((j)./(2*pi*60*Cm))); >> 100*abs(II^2) ans = 144.00

OpenStudy (raffle_snaffle):

% indices of max_C and max_L [max_C, max_L] = find(P_R == max_PR) % actual max_C and max_L values max_C = C_o(max_C) max_L = L_o(max_L) max_PR = 143.9982 max_C = 15 max_L = 10 Index exceeds matrix dimensions. Error in lab03 (line 32) max_C = C_o(max_C)

OpenStudy (phi):

you should read my post up above. your indices are switched.

OpenStudy (raffle_snaffle):

I just switched them. I did look at your post.

OpenStudy (raffle_snaffle):

[NewC_o, NewL_o] = meshgrid(C_o, L_o); % current output for RLC circuit (I) in RLC circuit (note: j = sqrt(-1)) I = (120)./(100 + j*2*pi*60*NewL_o - ((j)./(2*pi*60*NewC_o))); % power obsorbed by resistor P_R = 100*abs(I.^2); % defining new variable max PR max_PR = max(max(P_R)) % indices of max_C and max_L [max_C, max_L] = find(P_R == max_PR) % actual max_C and max_L values max_C = C_o(max_C) max_L = L_o(max_L)

OpenStudy (phi):

[L_ix , C_ix ] = find(P_R == max_PR) L_ix = 15 C_ix = 10 the actual values are L_o(L_ix) ans = 0.075000 C_o(C_ix) ans = 9.5000e-05

OpenStudy (raffle_snaffle):

okay give me a sec.

OpenStudy (raffle_snaffle):

[NewC_o, NewL_o] = meshgrid(C_o, L_o) L and C need to be swapped here?

OpenStudy (phi):

I would not change that. rather, when you use the find

OpenStudy (raffle_snaffle):

In the lab it said that the inductor vector needed to increase from left to right and the cap had to increase from top to bottom. So when I use the meshgrid command does it matter where L and C are in ==> [M, I] = meshgrid()

OpenStudy (raffle_snaffle):

max_L = 15 max_C = 10 ans = 0.0750 ans = 9.5000e-05

OpenStudy (phi):

inductor vector needed to increase from left to right that means L is going to be the rows and C the columns so I would do meshgrid(L,C)

OpenStudy (raffle_snaffle):

[NewL_o, NewC_o] = meshgrid(L_o, C_o) L_o(max_L) C_o(max_C) now it won't run it.

OpenStudy (phi):

you can do the problem with either L or C "increasing left to right" and you wrote your code to do the opposite of what your assignment asked. It will still give the correct answer, but it's not in the shape they wanted you to use. if you switch the order of the meshgrid, then you have to switch the order of the find.

OpenStudy (raffle_snaffle):

L_o = 0.005:0.005:0.245; % matrix L 2D matrix incr. from left to right C_o = 5e-6:10e-6:9.5e-5; % matrix C 2D matrix incr. from top to bottom The values for L will start at 0.005 and go to 0.245 in increments of 0.005. The values for C will start at 5e-6 and go to 9.5e-5 in increments of 10e-6 so my vectors are correct right?

OpenStudy (raffle_snaffle):

Yes those are correct.

OpenStudy (raffle_snaffle):

My L is increasing from left to right? Is it not?

OpenStudy (phi):

you can look at it in the command window I think they are talking about the meshgrid

OpenStudy (raffle_snaffle):

[NewL_o, NewC_o] = meshgrid(L_o, C_o) so L and C need to be swapped?

OpenStudy (raffle_snaffle):

M = NewC_o and I = NewL_o

OpenStudy (raffle_snaffle):

[M, I] = meshgrid()

OpenStudy (phi):

clc; clear % clears command window and workspace % defining input variables for RLC circuit f_volt = 60; % measured in hertz W_volt = 120; % measured in volts rms R = 100; % measured in ohms L_o = 0.005:0.005:0.245; % matrix L 2D matrix incr. from left to right C_o = 5e-6:10e-6:9.5e-5; % matrix C 2D matrix incr. from top to bottom [NewL_o, NewC_o] = meshgrid(L_o, C_o); % current output for RLC circuit (I) in RLC circuit (note: j = sqrt(-1)) I = (120)./(100 + j*2*pi*60*NewL_o - ((j)./(2*pi*60*NewC_o))); % power obsorbed by resistor P_R = 100*abs(I.^2); % defining new variable max PR max_PR = max(max(P_R)) % find max L and C power [C_ix, L_ix] = find(P_R == max_PR) max_C= C_o(C_ix), max_L= L_o(L_ix), % test these give the max power value II = (120)./(100 + j*2*pi*60*max_L - ((j)./(2*pi*60*max_C))); 100*abs(II^2),

OpenStudy (raffle_snaffle):

okay it runs now, but why we switch L and C here... [max_C, max_L] = find(P_R == max_PR)

OpenStudy (phi):

figuring out how indexing works is one of the more complicated things in matlab "find" returns two indices in some order. It turns out that is the order it uses. I keep track of how it works by using a "toy example" for example, using >> A= [ 1 2 3; 4 5 6] A = 1 2 3 4 5 6 >> [rr, cc]= find(A==6) rr = 2 cc = 3 the first index is the row, and the 2nd index is the column

OpenStudy (phi):

and >> A(rr,cc) ans = 6 returns the value

OpenStudy (raffle_snaffle):

Thanks phi for pointing that out.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!