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

matlab help!!!

OpenStudy (raffle_snaffle):

@phi

OpenStudy (raffle_snaffle):

%% 4.2 % Load the file thermo_scores.dat provided by your instructor, or enter % the matrix below and name it thermo_scores. (Enter only the numbers.) load('thermo'); % Loads the thermo_scores data % (a) Extract the scores and student number for student 5 into a row % vector named student_5. student_5 = data(5,:); % (b) Extract the scores for Test 1 into a column vector named test_1. test_1 = data(5,2); % (c) Find the standard deviation and variance for each test. Test_1 = data(:, 2); Test_2 = data(:, 3); Test_3 = data(:, 4); % standard deviation and variance for test scores 1-3 S1 = std(Test_1); S2 = std(Test_2); S3 = std(Test_3); % variance for tests 1-3 var1 = var(Test_1); var2 = var(Test_2); var3 = var(Test_3); % (d) Assuming that each test was worth 100 points, find each student's % final total score and final percentage. (Be careful not to add in the % student number.) total_sum1 = sum(data(1, 2:4)) total_sum2 = sum(data(2, 2:4)) total_sum3 = sum(data(3, 2:4)) total_sum4 = sum(data(4, 2:4)) total_sum5 = sum(data(5, 2:4)) total_sum6 = sum(data % (e) Create a table that includes the final percentages and the scores % from the original table.

OpenStudy (raffle_snaffle):

part d @phi

OpenStudy (raffle_snaffle):

so I could answer part d just making total_sum1 though total_sum20 but that seems like a waste of time.

OpenStudy (raffle_snaffle):

data = 1 68 45 92 2 83 54 93 3 61 67 91 4 70 66 92 5 75 68 96 6 82 67 90 7 57 65 89 8 5 69 89 9 76 62 97 10 85 52 94 11 62 34 87 12 71 45 85 13 96 56 45 14 78 65 87 15 76 43 97 16 68 76 95 17 72 65 89 18 75 67 88 19 83 68 91 20 93 90 92

OpenStudy (raffle_snaffle):

col 1 is number of students, col 2 is test score 1, col 2 is test score 2, and col 4 is test score 3

OpenStudy (raffle_snaffle):

col3 is test score 2

OpenStudy (phi):

rather than putting the sums into different variables, I would use a vector. Example ss=sum(data(:,2:4),2)

OpenStudy (raffle_snaffle):

@phi thank you so much.

OpenStudy (raffle_snaffle):

what does the 2 at the end indicate?

OpenStudy (phi):

do help sum

OpenStudy (phi):

you can put all the info into one matrix sc=[ data(:,1) ss ss/3]

OpenStudy (raffle_snaffle):

okay, sorry I was experimenting with the 2 at the end of the last command you showed me.

OpenStudy (raffle_snaffle):

why SS/3 ?

OpenStudy (phi):

I was assuming the final score was the total divided by 3 I guess we could do total/300 then multiply by 100 to change to percent. that works out to the same thing

OpenStudy (raffle_snaffle):

I see what you are saying. I misinterpreted the question. I thought total score equaled 100. My bad.

OpenStudy (phi):

I'm not sure what they want for score. It was just a guess if you want to print it out, you could do something like printf('Student total Score Percent\n'); printf(' %2d\t\t%5.1f\t%5.1f%% \n',sc');

OpenStudy (raffle_snaffle):

let me do that

OpenStudy (raffle_snaffle):

is it fprintf ? not printf

OpenStudy (phi):

try fprintf I'm using Octave, and there are slight differences It's not important, but I thought you might want to know about how to format printouts

OpenStudy (raffle_snaffle):

fprintf('data(:, 2:4), sum(data(:, 2:4), 2)), sum(data(:, 2:4), 2)) / 3\n')

OpenStudy (phi):

fprintf(' %2d\t\t%5.1f\t%5.1f%% \n',sc') what does that do ?

OpenStudy (raffle_snaffle):

do I need to replace 2d and 5.1f with my commands?

OpenStudy (raffle_snaffle):

fprintf(' %2d\t\t%5.1f\t%5.1f%% \n',sc') 1 205.0 68.3% 2 230.0 76.7% 3 219.0 73.0% 4 228.0 76.0% 5 239.0 79.7% 6 239.0 79.7% 7 211.0 70.3% 8 163.0 54.3% 9 235.0 78.3% 10 231.0 77.0% 11 183.0 61.0% 12 201.0 67.0% 13 197.0 65.7% 14 230.0 76.7% 15 216.0 72.0% 16 239.0 79.7% 17 226.0 75.3% 18 230.0 76.7% 19 242.0 80.7% 20 275.0 91.7% >>

OpenStudy (raffle_snaffle):

did you just assign variables?

OpenStudy (phi):

yes, that is what it's supposed to do. the "format" is given by for example %2d (2 digits of an integer) %5.1f 5 digits of a "float" including the decimal point and 1 digit to the right of the decimal the data in sc transposed (notice it's sc') matches up with the format statement. you can play with it when you have time.

OpenStudy (raffle_snaffle):

okay let me put that in. I've got one more question. It's my lab.

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-10:9.5e-5; % matrix C 2D matrix incr. from top to bottom [L_o, C_o] = meshgrid(L_o,C_o); % current output for RLC circuit (I) in RLC circuit (note: j = sqrt(-1)) I = (120)./(100 + 1i*2*pi*60*L_o - ((1i)./(2*pi*60*C_o))); % power obsorbed by resistor P_R = 100*abs(I.^2); % stores max power inside P_R matrix max_PR = max(P_R); Max_L = find(max(L_o)) % specifies maximum for inductor L Max_C = find(max(C_o))

OpenStudy (raffle_snaffle):

so the last little bit of the assignment. max_PR, max_L, and max_C. I just wanted to make sure I am heading in the right direction?

OpenStudy (phi):

depends. what are you trying to do ?

OpenStudy (raffle_snaffle):

I need max_power for each L and C within the matrix

OpenStudy (raffle_snaffle):

Use your results to report which specific value for L and value for C resulted in max PR. Store these two results in the variables max L and max C, respectively. The find function should be instrumental here. More specifically, see its usage near the top of p. 178 in your textbook.

OpenStudy (phi):

btw, j should also be pre-defined to equal sqr(-1)

OpenStudy (raffle_snaffle):

I have a note on the side. I used j instead but matlab didn't like that.

OpenStudy (raffle_snaffle):

% current output for RLC circuit (I) in RLC circuit (note: j = sqrt(-1))

OpenStudy (raffle_snaffle):

so my understanding for the last part is that I need to find the max power for the inductor and max power for the capacitor in the array/matrix?

OpenStudy (raffle_snaffle):

is my assumption right? Don't write my code for me I just want to make sure my logic is correct.

OpenStudy (phi):

are you sure? you may have overwritten it . but if you start matlab clean, j is defined

OpenStudy (raffle_snaffle):

hold on let me try that

OpenStudy (raffle_snaffle):

well it works... That is strange. For the longest time it wouldn't accept j. Matlab kept telling me to change it to 1i

OpenStudy (raffle_snaffle):

so back to my other question do I need to find max values for L and C in the power matrix

OpenStudy (phi):

Use your results to report which specific value for L and value for C resulted in max PR. you first need to find the max power you have to use max twice on your 2d power matrix also keep track of the indices

OpenStudy (raffle_snaffle):

why do I need to use max() twice on 2d power matrix?

OpenStudy (phi):

are you looking for the max value in the whole matrix ? max works on one dimension at a time

OpenStudy (raffle_snaffle):

but how do I distinguish the max power for L and C in my power matrix? Is this where the find() command comes into play? when I use max(P_R) it shows.... max_PR = Columns 1 through 11 134.8577 136.0632 137.1975 138.2564 139.2361 140.1328 140.9431 141.6636 142.2916 142.8244 143.2599 Columns 12 through 22 143.5963 143.8320 143.9662 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 Columns 23 through 33 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 Columns 34 through 44 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 144.0000 Columns 45 through 49 144.0000 144.0000 144.0000 144.0000 144.0000

OpenStudy (raffle_snaffle):

it max. to 144.000000

OpenStudy (phi):

you should call it with [val, ix]= max(P_R) val (printed out up above) is the max value in each of the 49 columns if you want the max of those 49 values you now do [val2, ix2]= max(val) val2 will have the 144 ix2 will have the column that it was in ix(ix2) will be the row index i.e. P_R( ix(ix2), ix2) should have a 144.0 in it. the double indexing can be confusing.

OpenStudy (raffle_snaffle):

Okay I got to run. When I return I will play around with double index. I watched a few vids on utube early to try to understand the general idea. I think what I am going to need to do is just break the matrix down.

OpenStudy (phi):

btw, I would make these different variables [L_o, C_o] = meshgrid(L_o,C_o); the L_o on the output is a 2D matrix and the L_o on the input is a vector. to retrieve your original values, it might be easier to use the vectors example L_o(ix2) and C_o(ix(ix2))

OpenStudy (raffle_snaffle):

% (d) Recall that both the max and min functions can return not only the % maximum values in a column, but also the element number where those % values occur. Use this capability to determine the values of times at % which the maxima and minima occur in each column. max(vector_thermo(:, 2:4));

OpenStudy (raffle_snaffle):

%% 4.6 % The area of a triangle is, area = 1/2*base*height (see Figure P4.6). % Find the area of a group of triangles whose base varies from 0 to 10 m % and whose height varies from 2 to 6 m. Choose an appropriate spacing % for your calculational variables. Your answer should be a % two-dimensional matrix. base = 0:2:10; % base vector in meters height = 2:0.75:6; % height vector in meters

OpenStudy (raffle_snaffle):

I am a little confused. So I am suppose to decide on increments so the matrix have same dim so I can multiply?

OpenStudy (raffle_snaffle):

or do I use meshgrid() command?

OpenStudy (phi):

you can use mesh grid [ bb, hh] = meshgrid(base, height); Area= 0.5.*bb.*hh

OpenStudy (phi):

though your height values leave out the max value of 6 (because 0.75 does not divide evenly into 6)

OpenStudy (raffle_snaffle):

that is funny I did that... But what got me was the 2d dim. That doesn't output as 2D

OpenStudy (raffle_snaffle):

So I was trying to do is suppress all dim. except two columns. however every column (output side) is the same answer.

OpenStudy (phi):

>> [bb,hh]= meshgrid(base,height) bb = 0 2 4 6 8 10 0 2 4 6 8 10 0 2 4 6 8 10 0 2 4 6 8 10 0 2 4 6 8 10 0 2 4 6 8 10 hh = 2.0000 2.0000 2.0000 2.0000 2.0000 2.0000 2.7500 2.7500 2.7500 2.7500 2.7500 2.7500 3.5000 3.5000 3.5000 3.5000 3.5000 3.5000 4.2500 4.2500 4.2500 4.2500 4.2500 4.2500 5.0000 5.0000 5.0000 5.0000 5.0000 5.0000 5.7500 5.7500 5.7500 5.7500 5.7500 5.7500 >> Area= 0.5.*bb.*hh Area = 0.00000 2.00000 4.00000 6.00000 8.00000 10.00000 0.00000 2.75000 5.50000 8.25000 11.00000 13.75000 0.00000 3.50000 7.00000 10.50000 14.00000 17.50000 0.00000 4.25000 8.50000 12.75000 17.00000 21.25000 0.00000 5.00000 10.00000 15.00000 20.00000 25.00000 0.00000 5.75000 11.50000 17.25000 23.00000 28.75000

OpenStudy (phi):

remember you have to use .* (dot times)

OpenStudy (raffle_snaffle):

I did. that isn't a two-dim matrix

OpenStudy (raffle_snaffle):

that is a 6x6 matrix

OpenStudy (phi):

whos Area Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== Area 6x6 288 double Total is 36 elements using 288 bytes

OpenStudy (phi):

6 x 6 is two-dimensional 1 x 6 or 6 x 1 is one dimensional

OpenStudy (phi):

or, in other words, you need two indices to get at a value Area(2,3) for example

OpenStudy (raffle_snaffle):

hold on i need to read something.

OpenStudy (raffle_snaffle):

dimension refers to the vector living in the space? This is starting to come back. when we have a 2x2 matrix it lives in R^2 ?

OpenStudy (raffle_snaffle):

R^2 = two-dimensional?

OpenStudy (phi):

yes R^2 or R x R is two-dimensions intuitively each dimension is perpendicular to all the other dimensions point, line, plane, cube it's hard to think beyond 3 D

OpenStudy (raffle_snaffle):

Okay thanks. If I have any further questions I will post them here.

OpenStudy (raffle_snaffle):

@jim_thompson5910

OpenStudy (raffle_snaffle):

%% 4.10 % Create a 6x6 magic matrix. V_mag = magic(6); % (a) What is the sum of each of the rows? sum(V_mag, 2); % (b) What is the sum of each of the columns? sum(V_mag); % (c) What is the sum of each of the diagonals? sum(diag(V_mag, 0)); sum(diag(V_mag, 1)); sum(diag(V_mag, 2)); sum(diag(V_mag, 3)); sum(diag(V_mag, 4)); sum(diag(V_mag, 5)); sum(diag(V_mag, -1)); sum(diag(V_mag, -2)); sum(diag(V_mag, -3)); sum(diag(V_mag, -4)); sum(diag(V_mag, -5));

OpenStudy (raffle_snaffle):

is there another way to solve part c?

OpenStudy (raffle_snaffle):

I wanted to find the sum of each diagonal outputting all at the same time

jimthompson5910 (jim_thompson5910):

let me think

jimthompson5910 (jim_thompson5910):

`diag(V_mag)` should return the elements along the diagonal `sum(diag(V_mag))` will then add up the diagonal elements

OpenStudy (raffle_snaffle):

yes I agree

OpenStudy (raffle_snaffle):

I am thinking too

OpenStudy (raffle_snaffle):

I need sum of each diagonal. There are 11 diagonals.

jimthompson5910 (jim_thompson5910):

what is the output when you type in `diag(V_mag)` ?

OpenStudy (raffle_snaffle):

>> diag(V_mag) ans = 35 32 2 17 14 11 >>

jimthompson5910 (jim_thompson5910):

oh nvm, you don't want just the main diagonal. I see what part (c) is asking now

OpenStudy (raffle_snaffle):

V_mag = 35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11

jimthompson5910 (jim_thompson5910):

ok how about `diag(V_mag,0)` post both V_mag and the result of whatever `diag(V_mag,0)` spits out

OpenStudy (raffle_snaffle):

>> diag(V_mag, 0) ans = 35 32 2 17 14 11

jimthompson5910 (jim_thompson5910):

ok it looks like 0 is the main diagonal

OpenStudy (raffle_snaffle):

yes, I agree.

jimthompson5910 (jim_thompson5910):

how about diag(V_mag, 1)

jimthompson5910 (jim_thompson5910):

I'm guessing V_mag doesn't change each time you run the script?

OpenStudy (raffle_snaffle):

that will display the next diagonal to the right of 0

OpenStudy (raffle_snaffle):

>> diag(V_mag, 1) ans = 1 7 22 10 16

OpenStudy (raffle_snaffle):

>> diag(V_mag, -1) ans = 3 9 33 12 18

OpenStudy (raffle_snaffle):

that is the diagonal below 0

jimthompson5910 (jim_thompson5910):

everything looks good

OpenStudy (raffle_snaffle):

Okay thanks. I figured out my next problem, but I have a question about it.

OpenStudy (raffle_snaffle):

%% 4.11 % Extract a 3x3 matrix from the upper left-hand corner of the magic % matrix you created in Problem 4.9. Is this also a magic matrix? V_mag(1:3, 1:3);

OpenStudy (raffle_snaffle):

why is it 1:3, 1:3 not 0:3, 0:3 ?

jimthompson5910 (jim_thompson5910):

I wonder if `diag(V_mag, :)` will work instead of having a number in the second argument, have a colon tell me what the output of that is

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!