Wright State University Lake Campus/2019-1/Matlab

From Wikiversity
Jump to navigation Jump to search

../Python . MATLAB/Cookbook

fragments[edit | edit source]

Matrix inversion[edit | edit source]

A = sym([2 -1 0; -1 2 -1; 0 -1 2]);

Multiplication[edit | edit source]

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:

CSVmaker[edit | edit source]

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:
About=['This attribution is for the question '...
    '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':
Nvariables=length(VariableList)%NO TOUCH THIS LINE

%Step 5: Enter units for the answer:

%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;
    a= f /mTot;

    %Step 7: Modify the magic word CsvRow as:
    % CsvRow = [[Answer, var1, var2, ect.]]
    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
fout=fopen([string '.csv'],'wt')

OutputText=[Author, Spaces,'\n'];
OutputText=[OutputText, Attribution, Spaces,'\n'];
OutputText=[OutputText, About, Spaces,'\n'];
OutputText=[OutputText, Question, Spaces,'\n'];

%Now we write the separation row:

for jj = [1:Nvariables]
ss=['__' VariableList{jj}];

for ii=[1:Nrenditions]
    for jj=[1:Nvariables+1]
        Line=[Line ss ',']; 
    Line=[Line '\n']
    OutputText=[OutputText Line];


Line=[string '\n']
Line=[Line 'Author: ' Author '\n']
Line=[Line 'Attribution: ' Attribution '\n']
Line=[Line 'About: ' About '\n\n']
Line=[Line 'Renditions:\n']

for ii=[1:Nrenditions]
    Line=[Line num2str(ii) '. '];
   for jj=[1:Nvariables]
        VariableTag=['__' VariableList{jj} ];
    Line=[Line QuestionX]
    Line=[Line '\n']
Line=[Line '\nAnswers\n']
for ii=[1:Nrenditions]
    Line=[Line num2str(ii) '. ' num2str(Csv(1,ii)) ' ' Units '\n'];
ftext=fopen([mfilename '.txt'],'w');

21:12, 21 February 2019 (UTC)[edit | edit source]

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.974358974 1.1 2.8 3.8
1.230769231 1.5 2.4 4.8
0.925 1.9 2.1 3.7
1.1 1.8 2.2 4.4
0.833333333 1.7 2.5 3.5
1.025641026 1.2 2.7 4

Generate question as CVS[edit | edit source]

Business template.svg
$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
Example where a=f/(m1+m2)

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.


  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.