EGR 103/Spring 2019/Minilab 4

From PrattWiki
Jump to navigation Jump to search

Troubleshooting

  • In older MATLAB, scripts were not allowed to have formal functions defined in them -- they could have one-line anonymous functions but not anything like
function out = fname(in)
Furthermore any formally defined function would need to be in its own file and the actual name of the function is given by the name of the function.
  • In older MATLAB, the top function in a script can call on other functions defined in the script - for example if there is a file called run_me.m that has:
function out = run_me(in)
out = sub_fun1(10) + sub_fun2(20) + in
end

function z = sub_fun1(a)
z = 2*a
end

function q = sub_fun2(p)
q = p+1
When you call run_me(5), the run_me function can see the two subordinate functions in the same file. Note, however, that you cannot directly call the subordinate functions from the command line.
  • 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

  • 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 data points.

4.5.2

  • 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.3

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.4

  • 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.

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.5

  • Note: when you define your function for curve fitting, the coefficients will be the first argument, followed by the independent variable, followed by the dependent variable. This is a slightly different order from Python.

Your definition line will likely be something like:

yeqn = @(coefs, x) coefs(1) * x .* exp(-(x / coefs(2)) + 1) / coefs(2)

Partial Answer

St: 2.837e+04
Sr: 1.116e+03
r2: 9.607e-01

Coefficients and graph should match what you got when you solved this in Python. Be sure you are using good initial guesses - bad initial guesses will result in a flat red line! If your statistical values are a little different, that may just be different initial guesses.

4.5.6

  • Be sure to look closely at the provided example. The graph should look like the one you got in Minilab 3.