EGR 103/Fall 2019/Minilab 4

From PrattWiki
Jump to navigation Jump to search

Minilab 4 is now optional during the last week of labs, but at some point you really should go through it sometime!

Important Notes!

  • For each problem, write a new MATLAB script
  • MATLAB script names MUST:
    • Start with a letter,
    • Include only letters, numbers, and maybe an underline, and
    • End in ".m"


Troubleshooting

  • If MATLAB tells you it can't find the file and brings up a dialogue box including the ability to change the folder, click the button to change the folder.

Typos

  • None yet!

4.5.1 - Chapra 8.2

  • Pretty straightforward; remember to use [ ] to create matrices and ; between rows.
  • Use matrix multiplication where multiplication is indicated.

Partial Answer

(1)
     5     8    15
     8     4    10
     6     0    10
(3)
     3    -2    -1
    -6     0     4
    -2     0    -2
(4)
    28    21    49
     7    14    49
    14     0    28
(5)
     3     6     1
(6)
    25    13    74
    36    25    75
    28    12    52
(7)
    54    76
    41    53
    28    38
(8)
     9     2
     4    -1
     3     7
    -6     5
(11)
    66    19    53
    19    29    46
    53    46   109
(12)
    46

4.5.2 - Chapra 2.16

  • Note that you are not trying to find model coefficients here - you already have the model including the coefficients. If you needed to find the coefficients, the code could be:
fun = @(coef, time) coef(1)*exp(coef(2)*t)
coefs = nlinfit(t, c, fun, [1, -.01])

which is fairly similar to how Python does nonlinear fitting but with a couple changes in syntax...

  • To get multiple lines on the same graph, you will plot the first one then issue the command hold on. Make the rest of your plots, then set hold off. For example:
plot(x, y1, 'k-')
hold on
plot(x, y2, 'r--')
plot(x, y3, 'g:')
hold off
  • For the legend, add labels to the legend command in the order you made the plots. For example:
legend('y1', 'y2', 'y3', 'location', 'best')
The last part tells MATLAB to put the legend in the best - or most clear - location.

Partial Answer

St: 5.793e+00
Sr: 7.850e-02
r2: 9.864e-01

The model should be a green dashed line going through the red diamond data points. Note that MATLAB's green is almost too light to be useful versus Python's green.

4.5.3 - Chapra 3.10

  • Be sure to create an anonymous function called u_y that returns a value. Since the return calculation is long, you will want to put it in multiple lines separated by ellipses (...). For instance, the following calculation is one line of code:
u_y = @(x) x.^3 + x.^2 + ...
    x.^1 + x.^0

Partial Answer

Root 1: 2.338e+00
Root 2: 7.762e+00
Min of y=-2.086e+01 at x=+1.279e+00
Min of y=-3.274e+01 at x=+8.708e+00
Min of y=-2.086e+01 at x=+1.279e+00
Max of y=+1.953e+02 at x=+5.702e+00

The graph should be like your graph from a previous lab for this problem.

4.5.4 - Chapra 3.26

  • This one takes a while to process; it is not the most efficient way of making this graph since it calls the plot command 10k times; far better to calculate the values and then use a scatter plot (not required)

Partial Answer

4.5.5 - Chapra 8.3 and 5

  • For complex numbers, write the imaginary part followed directly by a j with no space -- including if you are writing just j. Also, do not put spaces around the + or - between the real and imaginary parts - for instance:
a = 4+1j
Do not use i (it works, but still...) and do not forget to put the number in front of j, even if that number is a 1.
  • Use left divide to solve for $$x$$ in $$Ax=b$$.

Partial Answer

8.3: 
  -1.5181e+01
  -7.2464e+00
  -1.4493e-01
8.5:
  -5.3333e-01 + 1.4000e+00i
   1.6000e+00 - 5.3333e-01i

4.5.6 - Chapra 3.9

  • Don't forget to use .* ./ and .^ when needed

Partial Answer

  • Lab 8

4.5.7 - Sphere!

  • Don't forget to use .* ./ and .^ when needed

Partial Answer

  • Title of problem.