Wright State University Lake Campus/2019-1/Matlab/NEXT
Appearance
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: Break line with '...
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 __mass kg is placed on a horizontal spring '...
'with a spring constant of __ks N/m. The mass is placed on the '...
'spring after it is compressed by __mmBig mm. If there is no '...
'friction@ how fast is it moving when the compression is '...
'__mmSmall mm?'];
%Warning: Do not use commas anywhere in the question.
%>Step 4: Enter variable strings, in same order as above.
VARLISTSTRING={'mass','ks','mmBig','mmSmall'};
%....................................... MUST MATCH BELOW
NVARIABLES=length(VARLISTSTRING);%NO TOUCH!
%>Step 5: Select the number of renditions (typically 3-20):
NRENDITIONS=3;
for NN = [1:NRENDITIONS]
%>Step 6: Assign random variables and solve
mass = 1%randi([501 999])/10;
ks= 1000;
mmBig = 5;%randi([101 199])/100;
mmSmall = 2;%randi([201 399])/100;
%>Step 7: Rewrite VariableList of step 4 and as variables (not string):
VARLISTVARIABLE=[mass, ks, mmBig, mmSmall];
%....................................... MUST MATCH ABOVE
%>Step 8: Solve for ANSWER (caps denote "magic words")
x2=mmBig/1000;
x1=mmSmall/1000;
m=mass
U=.5*ks*x2^2
U=U-.5*ks*x1^2
vSquared=2*mass*U
ANSWER = vSquared^.5;
%>Step 9: Enter units for the answer:
UNITS='m/s';
% >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
% !!!! DONE !!!! DONE !!!! !!!! DONE !!!! DONE !!!!
CsvRow = [ANSWER VARLISTVARIABLE];
Csv=[Csv;CsvRow];
end
string=mfilename
fout=fopen([string '.csv'],'wt')
Spaces=[repmat(',',1,NVARIABLES+1)];
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:
string=UNITS;
for jj = [1:NVARIABLES]
ss=['__' VARLISTSTRING{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 'Attribution: ' ATTRIBUTION '\n']
Line=[Line 'About: ' ABOUT '\n\n']
Line=[Line 'Renditions:\n\n']
for ii=[1:NRENDITIONS]
Line=[Line num2str(ii) '. '];
QuestionX=QUESTION
for jj=[1:NVARIABLES]
VariableTag=['__' VARLISTSTRING{jj} ];
Vstring=num2str(Csv(ii,jj+1))
QuestionX=strrep(QuestionX,VariableTag,Vstring)
end
Line=[Line QuestionX]
Line=[Line '\n\n']
end
Line=[Line '\nAnswers\n']
for ii=[1:NRENDITIONS]
Line=[Line num2str(ii) '. ' num2str(Csv(ii,1)) ' ' UNITS '\n'];
end
ftext=fopen([mfilename '.txt'],'w');
fprintf(ftext,Line)
fclose(ftext)