-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalculus.js
More file actions
317 lines (303 loc) · 17.6 KB
/
Calculus.js
File metadata and controls
317 lines (303 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//Textbox parameters: (x,y,width,height,text size)
//Button parameters: (x,y,width,height,text,text size)
var calculusTextBox = new TextBox(320,260,350,40,30);
var calculusCheckButton = new Button(380,310,180,40,"Check",30,xOffset=50);
var calculusNewQuestionButton = new Button(370,360,220,40,"New Question",30,xOffset=15);
var singleDer = new CategorySwitch(335,535,390,50,[320,500,740,755],"Single Variable Derivatives",30,xOffset=17,onColor=[239, 143, 143]);
var singleInt = new CategorySwitch(335,605,390,50,[320,500,740,755],"Single Variable Integrals",30,xOffset=33,onColor=[239, 143, 143]);
var multiInt = new CategorySwitch(335,675,390,50,[320,500,740,755],"Multivariable Integrals",30,xOffset=45,onColor=[239, 143, 143]);
var calculusGenerateQuestion = true; calculusShowExplanation = false;
var integralsQuestion = ""; integralsXQuestion = ""; integralsYQuestion = ""; integralsZQuestion = "";
var integralsAnswer = null; integralsXAnswer = ""; integralsYAnswer = ""; integralsZAnswer = "";
var integralsExplanation = ""; integralsTempExplanation = ""; calculusFeedback="";
var integalsXNumericAnswer = 0; integalsYNumericAnswer = 0; integalsZNumericAnswer = 0;
var integralsXAnswerCoefficient = 0; integralsYAnswerCoefficient = 0; integralsZAnswerCoefficient = 0;
var integralsXQuestionCoefficient = 0; integralsYQuestionCoefficient = 0; integralsZQuestionCoefficient = 0;
var integralsType = 0; integralsNumeric = 0; temp1 = 0; temp2 = 0;
var integralsXSubType = 0; integralsYSubType = 0; integralsZSubType = 0;
var integralsXCoefficient = 0; integralsYCoefficient = 0; integralsZCoefficient = 0;
var lowerBound1 = 0; upperBound1 = 0; lowerBound2 = 0; upperBound2 = 0; lowerBound3 = 0; upperBound3 = 0;
var derivativesPowers = [2,3,4,5,6,7,8,9,10];
var calculusType = 0; //differentiation or integration
var derivativesQuestion = ""; derivativesAnswer = null;
var derivativesExplanation="";
function calculus(){
fill(255,238,153,100); noStroke(); rect(765,155,585,600,20,0,0,20);
fill(255,206,206); rect(320,475,420,260,20,20,20,20); stroke(0);
calculusTextBox.update();
calculusCheckButton.update();
calculusNewQuestionButton.update();
textSize(20);
text("Choose your difficulty: (SVD by default)",355,515); textSize(30);
if (calculusSwitch.switchedOn==true){
singleDer.update();
singleInt.update();
multiInt.update();
}
if(calculusGenerateQuestion == true){
//single or double or triple
if (singleInt.switchedOn==true){
calculusType=0;integralsType = 0;}
else if (multiInt.switchedOn==true){
calculusType=0;integralsType = Math.floor(random(1,3));}
else {calculusType=1;}
if (calculusType == 0){
//polynomial or e or sin or cos or const
integralsXSubType = Math.floor(random(0,5));
integralsYSubType = Math.floor(random(0,3));
integralsZSubType = Math.floor(random(0,3));
integralsXCoefficient = Math.floor(random(4,7));
integralsYCoefficient = Math.floor(random(4,7));
integralsZCoefficient = Math.floor(random(4,7));
//definite or indef
integralsNumeric = Math.floor(random(0,2));
lowerBound1 = Math.floor(random(-5,0));
upperBound1 = Math.floor(random(0,5));
lowerBound2 = Math.floor(random(-5,0));
upperBound2 = Math.floor(random(0,5));
lowerBound3 = Math.floor(random(-5,0));
upperBound3 = Math.floor(random(0,5));
integralsExplanation="Find the antiderivative F of the integrand with respect\n to x applying the constant rule:\n ";
integralsXQuestionCoefficient = integralsXCoefficient;
if (integralsXSubType == 0){integralsXAnswerCoefficient = 1;}
else {integralsXAnswerCoefficient = integralsXCoefficient;}
if (integralsXSubType == 0){
integralsXQuestion =/*integralsXCoefficient*/"(x^"+(integralsXCoefficient-1)+")";
integralsXAnswer = "(x^" + (integralsXCoefficient) + ")";
integralsXNumericAnswer= +(pow(upperBound1,integralsXCoefficient)-pow(lowerBound1,integralsXCoefficient)).toFixed(2);
} else if (integralsXSubType == 1){
integralsXQuestion = /*integralsXCoefficient*/ "(e^x)";
integralsXAnswer = /*integralsXCoefficient +*/ "(e^x)";
integralsXNumericAnswer = +(exp(upperBound1)-exp(lowerBound1)).toFixed(2);
} else if (integralsXSubType == 2){
integralsXQuestion = /*integralsXCoefficient*/ "sin(x)";
integralsXAnswer = /*"-" + integralsXCoefficient +*/ "cos(x)";
integralsXNumericAnswer= +(-cos(upperBound1)+cos(lowerBound1)).toFixed(2);
} else if (integralsXSubType == 3){
integralsXQuestion = /*integralsXCoefficient*/ "cos(x)";
integralsXAnswer = /*integralsXCoefficient +*/ "sin(x)";
integralsXNumericAnswer = +(sin(upperBound1)-sin(lowerBound1)).toFixed(2);
} else if (integralsXSubType == 4){
integralsXQuestion = /*integralsXCoefficient*/"";
integralsXAnswer = /*integralsXCoefficient +*/ "x";
integralsXNumericAnswer = +(upperBound1-lowerBound1).toFixed(2);
}
integralsYQuestionCoefficient = integralsYCoefficient;
if (integralsYSubType == 0){integralsYAnswerCoefficient = 1;}
else {integralsYAnswerCoefficient = integralsYCoefficient;}
if (integralsYSubType == 0){
integralsYQuestion=/*integralsYCoefficient*/"(y^"+(integralsYCoefficient-1)+")";
integralsYAnswer = "(y^" + integralsYCoefficient + ")";
integralsYNumericAnswer= +(pow(upperBound2,integralsYCoefficient)-pow(lowerBound2,integralsYCoefficient)).toFixed(2);
} else if (integralsYSubType == 1){
integralsYQuestion = /*integralsYCoefficient + */"(e^y)";
integralsYAnswer = /*integralsYCoefficient +*/ "(e^y)";
integralsYNumericAnswer = +(exp(upperBound2)-exp(lowerBound2)).toFixed(2);
} else if (integralsYSubType == 2){
integralsYQuestion = /*integralsYCoefficient*/"";
integralsYAnswer = /*integralsYCoefficient +*/ "y";
integralsYNumericAnswer = +(upperBound2-lowerBound2).toFixed(2);
}
integralsZQuestionCoefficient = integralsZCoefficient;
if (integralsZSubType == 0){integralsZAnswerCoefficient = 1;}
else {integralsZAnswerCoefficient = integralsZCoefficient;}
if (integralsZSubType == 0){
integralsZQuestion=/*integralsZCoefficient +*/"(z^"+(integralsZCoefficient-1)+")";
integralsZAnswer = "(z^" + integralsZCoefficient + ")";
integralsZNumericAnswer= +(pow(upperBound3,integralsZCoefficient)-pow(lowerBound3,integralsZCoefficient)).toFixed(2);
} else if (integralsZSubType == 1){
integralsZQuestion = /*integralsZCoefficient +*/ "(e^z)";
integralsZAnswer = /*integralsZCoefficient +*/ "(e^z)";
integralsZNumericAnswer = +(exp(upperBound3)-exp(lowerBound3)).toFixed(2);
} else if (integralsZSubType == 2){
integralsZQuestion = /*integralsZCoefficient*/"";
integralsZAnswer = /*integralsZCoefficient +*/ "z";
integralsZNumericAnswer = +(upperBound3-lowerBound3).toFixed(2);
}
//if the integral is double or triple, it's always definite
if (integralsType == 0){
integralsQuestion = (integralsXQuestionCoefficient) + integralsXQuestion;
if (integralsNumeric==0){
if (integralsXSubType == 0){
integralsAnswer = integralsXAnswer + " + C";
} else {
integralsAnswer=integralsXAnswerCoefficient+integralsXAnswer + " + C";
integralsXAnswer = integralsXAnswerCoefficient + integralsXAnswer; }
//add the - minus sign for the case when the integral is indefinite, single and sin
if(integralsXSubType==2){integralsAnswer="-"+integralsAnswer;integralsXAnswer="-"+integralsXAnswer;}
integralsExplanation+=integralsXAnswer+"\nThen add the constant:\n "+integralsAnswer;
} else {
integralsAnswer= +(integralsXAnswerCoefficient*integralsXNumericAnswer).toFixed(2);
if (integralsXSubType == 0){
integralsExplanation+=integralsXAnswer+
"\nThen use the Fundamental Theorem of Calculus with\n respect to x:\n F(b) - F(a) = "+
"F("+upperBound1+") - F("+lowerBound1+")\n = "+integralsAnswer;
} else {
integralsExplanation+=integralsXAnswerCoefficient+integralsXAnswer+
"\nThen use the Fundamental Theorem of Calculus with\n respect to x:\n F(b) - F(a) = "+
"F("+upperBound1+") - F("+lowerBound1+")\n = "+integralsAnswer;
}
}
}
if (integralsType == 1){
integralsQuestion=(integralsXQuestionCoefficient*integralsYQuestionCoefficient)+integralsXQuestion+integralsYQuestion;
integralsAnswer= +(integralsXAnswerCoefficient*integralsYAnswerCoefficient*integralsXNumericAnswer*integralsYNumericAnswer).toFixed(2);
if (integralsXSubType == 0){
temp1 = +(integralsYQuestionCoefficient).toFixed(2);
temp2 = +(integralsXNumericAnswer*integralsYQuestionCoefficient).toFixed(2);
} else {
temp1 = +(integralsXAnswerCoefficient*integralsYQuestionCoefficient).toFixed(2);
temp2 = +(integralsXNumericAnswer*integralsXAnswerCoefficient*integralsYQuestionCoefficient).toFixed(2);
}
integralsTempExplanation=temp1+integralsXAnswer+integralsYQuestion+
"\nThen use the Fundamental Theorem of Calculus with\n respect to x:\n "+
temp1+integralsYQuestion+"(F(b) - F(a)) = "+temp1+integralsYQuestion+
"(F("+upperBound1+") - F("+lowerBound1+"))\n = "+temp2+integralsYQuestion+"\n";
if (integralsYSubType == 0){
temp1 = +(integralsXAnswerCoefficient*integralsXNumericAnswer).toFixed(2);
} else {
temp1 = +(integralsXAnswerCoefficient*integralsXNumericAnswer*integralsYAnswerCoefficient).toFixed(2);
}
integralsTempExplanation+=
"Find the antiderivative F of the new integrand with\n respect to y applying the constant rule:\n "+
temp1+integralsYAnswer+"\nThen use the Fundamental Theorem of Calculus with\n respect to y:\n "+
temp1+"(F(b) - F(a)) = "+temp1+"(F("+upperBound2+") - F("+lowerBound2+
"))\n = "+integralsAnswer;
if (integralsXSubType == 2){
integralsExplanation += "-" + integralsTempExplanation;
} else { integralsExplanation += integralsTempExplanation; }
}
if (integralsType == 2){
integralsQuestion=(integralsXQuestionCoefficient*integralsYQuestionCoefficient*integralsZQuestionCoefficient)+integralsXQuestion+integralsYQuestion+integralsZQuestion;
integralsAnswer= +(integralsXAnswerCoefficient*integralsYAnswerCoefficient*integralsZAnswerCoefficient*integralsXNumericAnswer*integralsYNumericAnswer*integralsZNumericAnswer).toFixed(2);
if (integralsXSubType == 0){
temp1 = +(integralsYQuestionCoefficient*integralsZQuestionCoefficient).toFixed(2);
temp2 = +(integralsXNumericAnswer*integralsYQuestionCoefficient*integralsZQuestionCoefficient).toFixed(2);
} else {
temp1 = +(integralsXAnswerCoefficient*integralsYQuestionCoefficient*integralsZQuestionCoefficient).toFixed(2);
temp2 = +(integralsXNumericAnswer*integralsXAnswerCoefficient*integralsYQuestionCoefficient*integralsZQuestionCoefficient).toFixed(2);
}
integralsTempExplanation=temp1+integralsXAnswer+integralsYQuestion+integralsZQuestion+
"\nThen use the Fundamental Theorem of Calculus with\n respect to x:\n "+
temp1+integralsYQuestion+integralsZQuestion+"(F(b) - F(a)) = "+temp1+
integralsYQuestion+integralsZQuestion+"(F("+upperBound1+") - F("+lowerBound1+
"))\n = "+temp2+integralsYQuestion+integralsZQuestion+"\n";
if (integralsYSubType == 0){
temp1 = +(integralsXAnswerCoefficient*integralsXNumericAnswer*integralsZQuestionCoefficient).toFixed(2);
temp2 = +(integralsXAnswerCoefficient*integralsXNumericAnswer*integralsYNumericAnswer*integralsZQuestionCoefficient).toFixed(2);
} else {
temp1 = +(integralsXAnswerCoefficient*integralsXNumericAnswer*integralsYAnswerCoefficient*integralsZQuestionCoefficient).toFixed(2);
temp2 = +(integralsXAnswerCoefficient*integralsXNumericAnswer*integralsYAnswerCoefficient*integralsYNumericAnswer*integralsZQuestionCoefficient).toFixed(2);
}
integralsTempExplanation+=
"Find the antiderivative F of the new integrand with\n respect to y applying the constant rule:\n "+
temp1+integralsYAnswer+integralsZQuestion+"\nThen use the Fundamental Theorem of Calculus with\n respect to y:\n "+
temp1+integralsZQuestion+"(F(b) - F(a)) = "+temp1+integralsZQuestion+"(F("+
upperBound2+") - F("+lowerBound2+"))\n = "+temp2+integralsZQuestion+"\n";
if (integralsZSubType == 0){
temp1 = +(integralsXAnswerCoefficient*integralsYAnswerCoefficient*integralsXNumericAnswer*integralsYNumericAnswer).toFixed(2);
} else {
temp1 = +(integralsXAnswerCoefficient*integralsYAnswerCoefficient*integralsXNumericAnswer*integralsYNumericAnswer*integralsZAnswerCoefficient).toFixed(2);
}
integralsTempExplanation+="Find the antiderivative F of the new integrand with\n respect to z applying the constant rule:\n "+
temp1+integralsZAnswer+"\nThen use the Fundamental Theorem of Calculus with\n respect to z:\n "+
temp1+"(F(b) - F(a)) = "+temp1+"(F("+upperBound3+") - F("+lowerBound3+"))\n = "+integralsAnswer;
if (integralsXSubType == 2){
integralsExplanation += "-" + integralsTempExplanation;
} else { integralsExplanation += integralsTempExplanation; }
}
if (integralsType>=0){integralsQuestion += "dx"};
if (integralsType>=1){integralsQuestion += "dy"};
if (integralsType==2){integralsQuestion += "dz"};
} else {
var rand = Math.floor(Math.random() * 100);
if(rand<50){
var temp1 = derivativesPowerRule();
derivativesQuestion=temp1[0];
derivativesAnswer=temp1[1];
derivativesExplanation=temp1[2];
}else{
var pr = derivativesPowerRule();
var temp2 = derivativesChainRule(pr[0],pr[1]);
derivativesQuestion=temp2[0];
derivativesAnswer=temp2[1];
derivativesExplanation=temp2[2];
}
}
calculusGenerateQuestion = false;
}
/*If the integralsNewQuestionButon is clicked, set calculusGenerateQuestion
to true, so it generates a new question in the next frame*/
if(calculusNewQuestionButton.clicked==true){
calculusTextBox.data="";
calculusGenerateQuestion = true;
calculusShowExplanation = false; }
/*check if the user's answer is right or not when the calculusCheckButton is clicked*/
if(calculusCheckButton.clicked==true){
calculusShowExplanation = true;
if((calculusTextBox.data)==integralsAnswer||(calculusTextBox.data)==derivativesAnswer){calculusFeedback="Correct!";addPoints(35);}
else {calculusFeedback="Try again";}
}
function derivativesPowerRule(){
var question = ""; answer = ""; n = Math.floor(Math.random() * 3+1)
for(var i = 0;i<n;i++){
/*this picks a random number from the derivativesPowers list, then remove that number from the list,
so it won't be picked again.*/
var powerSelection = Math.floor(random(0,derivativesPowers.length));
var rand = derivativesPowers[powerSelection];
derivativesPowers.splice(powerSelection,1);
question+="x^" + rand; answer += ""+rand + "x^" + (rand-1);
if(i != (n-1)){ question+="+"; answer+="+"; }
}
derivativesPowers = [2,3,4,5,6,7,8,9,10];
var explanation = "Power & Addition Rules:\n"+answer;
return [question,answer,explanation];
}
function derivativesChainRule(insideQuestion,insideAnswer){
var question = ""; answer = ""; explanation = "";
var rand = Math.floor(Math.random() * 100);
if(rand<=33){
question="sin("+insideQuestion+")";
answer="cos("+insideQuestion+")("+insideAnswer+")"
explanation = "Step 1- Chain Rule:\ncos("+insideQuestion+")*d/dx("+insideQuestion+")"+
"\n\nStep 2- Power & Addition Rules:\n"+answer;
}else{
question="ln("+insideQuestion+")";
answer="("+insideAnswer+")"+"/("+insideQuestion+")";
explanation = "Step 1- Chain Rule:\n1/("+insideQuestion+")*d/dx("+insideQuestion+")"+
"\n\nStep 2- Power & Addition Rules:\n"+answer;
}
return [question,answer,explanation];
}
//Display the output, feedback, and explanation
text("Feedback: ",320,440);
text("Work/Explanation:",800,200);
if (calculusType==0){
if (calculusShowExplanation){
textSize(20); text(integralsExplanation,800,230); textSize(30);
text(calculusFeedback,470,440); }
text("Evaluate the following integral: (Round to 2 decimal places)",320,120);
//print integral signs and bounds
if (integralsType>=0){
textSize(60); text("∫",385,210);
if (integralsNumeric==1 || integralsType >=1){
textSize(18); text(lowerBound1,400,235); text(upperBound1,418,170); }
textSize(30);
}
if (integralsType>=1){
textSize(60); text("∫",355,210); textSize(18);
text(lowerBound2,370,235); text(upperBound2,388,170); textSize(30); }
if (integralsType==2){
textSize(60); text("∫",325,210); textSize(18);
text(lowerBound3,340,235); text(upperBound3,358,170); textSize(30); }
textSize(28); text(integralsQuestion,415,210); textSize(30);
} else {
text("Find the derivative:",320,120); text(" d\n"+"dx",340,180);
strokeWeight(2);line(340,190,370,190);strokeWeight(1); //reset to default value
text("("+derivativesQuestion+") = ?",375,195);
if (calculusShowExplanation){
text(calculusFeedback,470,440);
text(derivativesExplanation,800,245);
}
}
}