%% BME 200
%
% Erase this sentence and type your name.
%%
%% In-class worksheet/HW
% Aug 27, 2020
% Due: Sept 1, 2020, by beginning of class
%%
%% Introduction
% The purpose of this Live Script is to introduce you to basic Matlab concepts. Make sure you fully understand each concept before moving on. If you have questions, ask!
%
%% Defining variables
% You define a variable in Matlab like this:

a = 4;

% In words, we are telling Matlab to make the variable a equal to the value 4. The variable goes first, then the equals, then what you want to set the variable to. End the line with a semi-colon if you don't want Matlab to print the result. If you forget a semi-colon, Matlab will print the result of the line on the screen, like this:

a = 5

% You can set a variable equal to another variable

a = 4;
b = a

% The above code sets a equal to 4 then sets b equal to a.
% Matlab recognizes scientific notation. You can write the speed of light (3\times 10^8m/s) like this:

c = 3e8

%% Exercise 1
% Type in lines of code that do the following
% (1)Set a equal to 13. Set b equal to 31. Set b equal to a. Print the new value of b.
% (2)Assign Avogadro's number to a variable.

% Enter your code here then run by clicking the green Run button up top
% When you are done, click the green Run button on top of the window
% 
% If you start a line with a %, Matlab will ignore that line, so use % for
% your comments

% Problem 1

% Problem 2

%% Doing math with Matlab
% Matlab lets you do calculations. The operators you need to know for now are multiply (*), subtract (-), divide (/), and add (+). To raise a variable to a power, use ^ (as in 10^2 for 100 ). For example, I want to calculate x given the equation x=\frac{ab}{cd}. I need to define the variables then Matlab will do the work:

a = 1;
b = 2;
c = 3;
d = 4;

x = (a*b)/(c*d)

% Notice that we used parentheses to control the order of calculation. If we did not use parentheses, we would get a different answer:

x = a*b/c*d

% Matlab interprets this second equation as x=a\times\frac{b}{c}\times d, not x=\frac{ab}{cd}. When in doubt, just use parentheses to control what gets calculated first.
%% Exercise 2
% Type in lines of code that do the following. Run the code to check your answer
% (1) Ohm's law is V = I*R. Calculate the current if V = 5 and R = 3.1
% (2) Use the quadratic formula to find the roots of the polynomial 12x^2+6x-1=0. The quadratic formula is x=-\frac{b\pm\sqrt{b^2-4ac}}{2a}. Use sqrt() to find the square root of a number in Matlab. You will need two equations to find the roots (one for b + and one for b -).

% Enter your code here
% When you are done, click the green Run button on top of the window

% Problem 1

% Problem 2



%% Working with lists
% Now lets make Matlab work a little harder. Matlab can work with lists of numbers instead of single numbers. Define a list like this:
% variable = (starting number : ending number)

a = (1:10)

% Even better, Matlab can calculate equations using lists of numbers. Say we want to calculate the position of a ball on a pool table from 1 to 7 seconds. Assume the equation for position is x=4t where t is the variable for time. Then we can do this:

t = (1:7);
x = 4*t

% Now we have the position for every second. But what if we want to know the position every 1/10th of a second? We can redefine t in increments of 0.1 seconds like this:

t = (1:0.1:7)

% and then calculate x again:

x = 4*t

% Now we have 61 values for position instead of just 7. Here's how to specify what increment to use in your list:
% variable = (starting number : increment : ending number)
%
%% Exercise 3
% Type in lines of code that do the following. Run the code to check your answer
% (1) Calculate the square of all integers from 3 to 13 and assign the answer to the variable y.
% (2) Add the list of integers from 30 to 35 to the list of integers from 75 to 80.
% (3) Ohm's law is V = I*R. Assume R = 17. Calculate the voltage for all currents from 3 to 5 Amps, in increments of 0.2 Amps.

% Enter your code here
% When you are done, click the green Run button on top of the window

% Problem 1


% Problem 2



% Problem 3


%
%% Plotting
% We can make a simple plot by telling Matlab the following:
% The x-axis (independent) values
% The y-axis (dependent) values; or, by giving matlab a formula
% What type of plot to make
% Here's a simple plot of time versus position for the pool ball from the previous example.

t = (0:7);  % the time variable, t, has values from 0 to 7
x = 4*t;    % a corresponding x value is calculated for each t

plot(x,t);  % plot command to plot t (x-axis) vs x (y-axis); x-axis variable goes first in plot(x,t)

%Formatting for plot
title('Position vs. Time');  % plot label
xlabel('Time (sec)'); % name of x-axis and units
ylabel('Position (m)') % name of y-axis and units

% We did not calculate many values, just 7, so where did the line come from? Matlab interpolated (drew line segments between) the values that it calculated. If we wanted to plot only the data points that Matlab calculated, we change the "plot" command slightly:
% plot(x-axis~values, y-axis~values, symbol)

t = (0:7);
x = 4*t;

plot(x,t,'o'); % here we're telling matlab to plot t vs x and to use "o" for each point

%Formatting for plot
title('Position vs. Time');  % plot label
xlabel('Time (sec)'); % name of x-axis and units
ylabel('Position (m)') % name of y-axis and units

% Now we've got three pool balls and we want to plot them all on the same graph. How do we do this? Assign an equation to each ball:

t = (0:7);
p1 = 4*t;   % equation for first pool ball
p2 = 6*t;   % equation for second pool ball
p3 = 0.3*t; % equation for third pool ball

plot(p1,t,p2,t,p3,t);

%Formatting for plot
legend('Ball 1','Ball 2','Ball 3','Location','southeast'); % give labels to plots and specify location; southeast = lower-right corner
title('Position vs. Time');  % plot label
xlabel('Time (sec)'); % name of x-axis and units
ylabel('Position (m)') % name of y-axis and units

% Let's plot y=x^6 from x = 1 to 3. Note that we must use ".^" to square x, since we are squaring values in a list instead of individually.

x = (0:3);
y = x.^6;
plot(x,y)

% Notice how the curve looks like three line segments joined together. That's exactly what it is because we only calculated y values for the x values 0, 1, 2, and 3. Then Matlab connected the dots with lines. To make the curve smooth, you can add more y values by using smaller increments for x, like this:

x = (0:0.1:3);
y = x.^6;
plot(x,y)

%%
%% Student exercise 4
% Type in lines of code that do the following. Run the code to check your answer
% (1) Plot the amount of energy present in masses ranging from 0 kg to 25 kg (in increments of 1 kg) using Einstein's equation E=mc^2. The variable m is the mass and c is the speed of light. Be sure to label your axes. E is in Joules (J).
% (2) Plot the equations y=0.5x^2 and y=0.25x^3 on the same plot for values of x from 1 to 3. Make them look smooth and curvy!
% (3) Repeat #2, but plot using 'o' for y=0.5x^2 and '+' for y=0.25x^3.

% Enter your code here
% When you are done, click the green Run button on top of the window

% Problem 1


% Problem 2


% Problem 3


%