# Wright State University Lake Campus/2019-1/Matlab

## fragments

### Matrix inversion

```%https://www.mathworks.com/help/symbolic/inv.html
A = sym([2 -1 0; -1 2 -1; 0 -1 2]);
inv(A)
```

### Multiplication

```clc;clear all;close all;format compact;
a=[1:3]%makes [1,2,3] (3 columns)
b=randi([4,7],3,1)%makes 3 rows one column
a*b %makes a dot product (scalar) aka MATRIX MULTIPLICATION
'another type of product uses a.*c'
a=2*a %I did this just to print out a new a
c=randi([4,7],1,3)%makes 3 columns (like a)
%a*c crashes, but a.*c multiplies for each member of vector:
a.*c
```

### CSVmaker

```clc; format compact;clear,Csv=[];

%Step 1: Enter author's name and attribution as strings:
Author='Guy vandegrift';
Attribution='C00';%typically C00 (Public Domain) or CC-BY-SA

%Step 2: Make an optional comment:
'The MatLab code that expresses this question is released '...
'to the public domain.'];

%Step 3: Enter  question, using * before variables:
Question=['A mass of __m1 kg is attached to a mass of __m2 kg and '...
'experiences a force of __f N.  What is the acceleration?'];
%Warning: Do not use * anywhere else in the question.

%Step 4: Enter variable names, in same order as above.
%     Omit the * prefix but include single quotes 'x':
VariableList={'m1','m2','f'};
Nvariables=length(VariableList)%NO TOUCH THIS LINE

%Step 5: Enter units for the answer:
Units='m/s/s';

%Step 6: Select the number of renditions:
Nrenditions=3; %20 is a typical choice

for nRend = [1:Nrenditions]

%Step 6: Assign random variables and solve

f = randi([501 999])/10;
m1 = randi([101 199])/10;
m2 = randi([201 399])/10;
mTot=m1+m2;
a= f /mTot;

%Step 7: Modify the magic word CsvRow as:
% CsvRow = [[Answer, var1, var2, ect.]]
'CsvRow';
CsvRow = [[a f m1 m2]];

% Make sure your variables are in the proper order

%Step 8: DONE! Run the code and see what happens!
%There is no need to modify anything below this line
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Csv=[Csv;CsvRow];

end
string=mfilename
fout=fopen([string '.csv'],'wt')

Spaces=[repmat(',',1,Nvariables+1)];
OutputText=[Author, Spaces,'\n'];
OutputText=[OutputText, Question, Spaces,'\n'];

%Now we write the separation row:
string=Units;

for jj = [1:Nvariables]
ss=['__' VariableList{jj}];
string=[string,',',ss];
end
OutputText=[OutputText,string,',\n']

for ii=[1:Nrenditions]
Line='';
for jj=[1:Nvariables+1]
ss=num2str(Csv(ii,jj))
Line=[Line ss ','];
end
Line=[Line '\n']
OutputText=[OutputText Line];
end

fprintf(fout,OutputText);
fclose(fout)

string=mfilename;
Line=[string '\n']
Line=[Line 'Author: ' Author '\n']
Line=[Line 'Renditions:\n']

for ii=[1:Nrenditions]
Line=[Line num2str(ii) '. '];
QuestionX=Question
for jj=[1:Nvariables]
VariableTag=['__' VariableList{jj} ];
Vstring=num2str(Csv(ii,jj+1))
QuestionX=strrep(QuestionX,VariableTag,Vstring)
end
Line=[Line QuestionX]
Line=[Line '\n']
end
for ii=[1:Nrenditions]
Line=[Line num2str(ii) '. ' num2str(Csv(1,ii)) ' ' Units '\n'];
end
ftext=fopen([mfilename '.txt'],'w');
fprintf(ftext,Line)
fclose(ftext)
```

#### 21:12, 21 February 2019 (UTC)

Goal: combine previous two matlab codes to create a csv file that excel can open. It will contain only numbers. It will look like this:

 0.974359 1.1 2.8 3.8 1.23077 1.5 2.4 4.8 0.925 1.9 2.1 3.7 1.1 1.8 2.2 4.4 0.833333 1.7 2.5 3.5 1.02564 1.2 2.7 4

## Generate question as CVS

 \$m/s^2\$ __m1 __m2 __f 1.13646851 19.3 39.3 862 1.572588631 15.3 33 794 0.199812788 24.8 44.8 222 0.916104147 24.4 40.8 912 1.829147716 10.4 38.9 740 0.665022099 23 42.3 647 0.958669709 11.2 40.7 437 0.916317805 20.6 43.6 823 1.262364849 20.7 33.6 878

Goal: Use MatLab to generate about 20 renditions of the calculation for a numerical problem, and print the results in a CSV file that Python can use to create the LaTeX markup that can be processed by the quiz bank. Even though MatLab handles Excel files, Python works better with CSV. Excel can easily read and create CSV files.

1. The csv files will contain only answers and input variables.
2. The names of the input variables will be placed in the top row of the CSV spreadsheet.
3. Input variables will be rounded to three significant figures.
4. Each rendition will consist of one line: Answer in column 1 and question variables in the other columns. Should we place "end" after the last variable to avoid unwanted elements?
5. The top row of column 1 will contain the units.
6. Use a comment in the MatLab file to state the question in words. In the example shown, you might use
`%What is the acceleration if a force of f-N is applied to two attached objects of mass m1-kg and m2-kg?`
7. I will write the Python code that renders this into a LaTex (tex) file for the bank.

Steps:

1. Write a proposal for this project that might attract others to join and/or give us moola. Assume the reader is unaware of the Quizbank project. Make the proposal between one and two pages long. In your proposal, point out that it is not necessary to use MatLab to prepare such spreadsheets. Even Excel could do many if not most problems of this nature.
2. Learn to read and write a cvs file with MatLab.
3. Find a way to create random variables that can be exactly expressed using 3 significant figures (we want there to exist an exact answer for each posed problem.)
4. Write a practice problem (like the easy f=ma problem shown above).
5. Write lots of real problems, using odd problems from the textbook.
6. Optinal: Write a MatLab code that creates the LaTeX (tex) file. You need not be familiar with LaTeX because that part is easy to do, even in MatLab.