ShivaamShukla commited on
Commit
7439261
·
verified ·
1 Parent(s): 93ae1fe

add all the Calculus related things in calculator

Browse files
Files changed (1) hide show
  1. index.html +177 -2
index.html CHANGED
@@ -100,8 +100,9 @@
100
  <button id="trigMode" class="mode-selector px-4 py-2 rounded-lg">Trigonometry</button>
101
  <button id="matrixMode" class="mode-selector px-4 py-2 rounded-lg">Matrix</button>
102
  <button id="scientificMode" class="mode-selector px-4 py-2 rounded-lg">Scientific</button>
 
103
  </div>
104
- <!-- Display -->
105
  <div class="result-display">
106
  <div class="text-right text-gray-500 text-sm h-6 overflow-x-auto whitespace-nowrap" id="historyDisplay"></div>
107
  <div class="text-right text-3xl font-bold mt-2 h-12 overflow-x-auto whitespace-nowrap" id="mainDisplay">0</div>
@@ -326,10 +327,47 @@
326
  </div>
327
  </div>
328
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329
  </div>
330
  </div>
331
 
332
- <!-- History and Info Section -->
333
  <div>
334
  <div class="bg-white rounded-2xl p-6 shadow-xl mb-8 border border-gray-200">
335
  <h2 class="text-xl font-bold mb-4 flex items-center">
@@ -374,6 +412,7 @@
374
  let currentInput = '0';
375
  let calculationHistory = [];
376
  let currentMode = 'basic';
 
377
 
378
  // DOM Elements
379
  const mainDisplay = document.getElementById('mainDisplay');
@@ -385,6 +424,7 @@
385
  generateMatrixInputs(3);
386
  updateDisplay();
387
  loadHistory();
 
388
  });
389
  // Mode switching
390
  document.querySelectorAll('.mode-selector').forEach(button => {
@@ -399,10 +439,16 @@
399
  document.getElementById('trigCalculator').classList.add('hidden');
400
  document.getElementById('matrixCalculator').classList.add('hidden');
401
  document.getElementById('scientificCalculator').classList.add('hidden');
 
402
 
403
  // Show selected calculator
404
  currentMode = this.id.replace('Mode', '');
405
  document.getElementById(currentMode + 'Calculator').classList.remove('hidden');
 
 
 
 
 
406
  });
407
  });
408
  // Basic calculator functions
@@ -690,6 +736,135 @@ function clearMatrix() {
690
  }
691
  document.getElementById('matrixResult').innerHTML = 'Result will appear here';
692
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
693
  // History functions
694
  function addToHistory(expression, result) {
695
  const historyItem = {
 
100
  <button id="trigMode" class="mode-selector px-4 py-2 rounded-lg">Trigonometry</button>
101
  <button id="matrixMode" class="mode-selector px-4 py-2 rounded-lg">Matrix</button>
102
  <button id="scientificMode" class="mode-selector px-4 py-2 rounded-lg">Scientific</button>
103
+ <button id="calculusMode" class="mode-selector px-4 py-2 rounded-lg">Calculus</button>
104
  </div>
105
+ <!-- Display -->
106
  <div class="result-display">
107
  <div class="text-right text-gray-500 text-sm h-6 overflow-x-auto whitespace-nowrap" id="historyDisplay"></div>
108
  <div class="text-right text-3xl font-bold mt-2 h-12 overflow-x-auto whitespace-nowrap" id="mainDisplay">0</div>
 
327
  </div>
328
  </div>
329
  </div>
330
+ <!-- Calculus Calculator -->
331
+ <div id="calculusCalculator" class="hidden">
332
+ <div class="result-display">
333
+ <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-4">
334
+ <div>
335
+ <label class="block text-gray-700 mb-2">Function f(x)</label>
336
+ <input type="text" id="functionInput" class="w-full p-3 rounded-lg bg-gray-100 text-black" placeholder="e.g., x^2 + 3*x + 2" value="x^2">
337
+ </div>
338
+ <div>
339
+ <label class="block text-gray-700 mb-2">Point (for derivative)</label>
340
+ <input type="number" id="pointInput" class="w-full p-3 rounded-lg bg-gray-100 text-black" placeholder="Enter x-value" value="2">
341
+ </div>
342
+ </div>
343
+ <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4">
344
+ <button class="calculator-btn bg-primary hover:bg-indigo-600 text-white font-bold py-3 rounded-xl" onclick="calculateDerivative()">Derivative</button>
345
+ <button class="calculator-btn bg-primary hover:bg-indigo-600 text-white font-bold py-3 rounded-xl" onclick="calculateIntegral()">Integral</button>
346
+ <button class="calculator-btn bg-gray-700 hover:bg-gray-600 text-white font-bold py-3 rounded-xl" onclick="clearCalculus()">Clear</button>
347
+ </div>
348
+ <div class="bg-gray-100 rounded-xl p-4">
349
+ <div class="flex justify-between mb-2">
350
+ <span>Derivative f'(x):</span>
351
+ <span id="derivativeResult" class="font-bold">2*x</span>
352
+ </div>
353
+ <div class="flex justify-between mb-2">
354
+ <span>f'(<span id="pointValue">2</span>) =</span>
355
+ <span id="derivativeAtPoint" class="font-bold">4</span>
356
+ </div>
357
+ <div class="flex justify-between">
358
+ <span>∫f(x)dx =</span>
359
+ <span id="integralResult" class="font-bold">(1/3)*x^3 + C</span>
360
+ </div>
361
+ </div>
362
+ <div class="mt-4">
363
+ <canvas id="functionChart" height="200"></canvas>
364
+ </div>
365
+ </div>
366
+ </div>
367
  </div>
368
  </div>
369
 
370
+ <!-- History and Info Section -->
371
  <div>
372
  <div class="bg-white rounded-2xl p-6 shadow-xl mb-8 border border-gray-200">
373
  <h2 class="text-xl font-bold mb-4 flex items-center">
 
412
  let currentInput = '0';
413
  let calculationHistory = [];
414
  let currentMode = 'basic';
415
+ let functionChart = null;
416
 
417
  // DOM Elements
418
  const mainDisplay = document.getElementById('mainDisplay');
 
424
  generateMatrixInputs(3);
425
  updateDisplay();
426
  loadHistory();
427
+ initializeChart();
428
  });
429
  // Mode switching
430
  document.querySelectorAll('.mode-selector').forEach(button => {
 
439
  document.getElementById('trigCalculator').classList.add('hidden');
440
  document.getElementById('matrixCalculator').classList.add('hidden');
441
  document.getElementById('scientificCalculator').classList.add('hidden');
442
+ document.getElementById('calculusCalculator').classList.add('hidden');
443
 
444
  // Show selected calculator
445
  currentMode = this.id.replace('Mode', '');
446
  document.getElementById(currentMode + 'Calculator').classList.remove('hidden');
447
+
448
+ // Update chart when switching to calculus mode
449
+ if (currentMode === 'calculus') {
450
+ updateChart();
451
+ }
452
  });
453
  });
454
  // Basic calculator functions
 
736
  }
737
  document.getElementById('matrixResult').innerHTML = 'Result will appear here';
738
  }
739
+ // Calculus functions
740
+ function initializeChart() {
741
+ const ctx = document.getElementById('functionChart').getContext('2d');
742
+ functionChart = new Chart(ctx, {
743
+ type: 'line',
744
+ data: {
745
+ labels: [],
746
+ datasets: [{
747
+ label: 'f(x)',
748
+ data: [],
749
+ borderColor: '#6366f1',
750
+ backgroundColor: 'rgba(99, 102, 241, 0.1)',
751
+ borderWidth: 2,
752
+ fill: true,
753
+ tension: 0.4
754
+ }]
755
+ },
756
+ options: {
757
+ responsive: true,
758
+ maintainAspectRatio: false,
759
+ scales: {
760
+ x: {
761
+ title: {
762
+ display: true,
763
+ text: 'x'
764
+ }
765
+ },
766
+ y: {
767
+ title: {
768
+ display: true,
769
+ text: 'f(x)'
770
+ }
771
+ }
772
+ }
773
+ }
774
+ });
775
+ }
776
+
777
+ function updateChart() {
778
+ const funcStr = document.getElementById('functionInput').value || 'x^2';
779
+ const points = [];
780
+ const values = [];
781
+
782
+ // Generate data points
783
+ for (let x = -10; x <= 10; x += 0.5) {
784
+ try {
785
+ const expr = funcStr.replace(/\^/g, '**').replace(/x/g, x);
786
+ const y = eval(expr);
787
+ if (isFinite(y)) {
788
+ points.push(x);
789
+ values.push(y);
790
+ }
791
+ } catch (e) {
792
+ // Skip invalid points
793
+ }
794
+ }
795
+
796
+ functionChart.data.labels = points;
797
+ functionChart.data.datasets[0].data = values;
798
+ functionChart.data.datasets[0].label = `f(x) = ${funcStr}`;
799
+ functionChart.update();
800
+ }
801
+
802
+ function calculateDerivative() {
803
+ const funcStr = document.getElementById('functionInput').value || 'x^2';
804
+ const point = parseFloat(document.getElementById('pointInput').value) || 2;
805
+
806
+ // Update point display
807
+ document.getElementById('pointValue').textContent = point;
808
+
809
+ // Simple derivative calculation using numerical differentiation
810
+ const h = 1e-5;
811
+ const expr1 = funcStr.replace(/\^/g, '**').replace(/x/g, `(${point + h})`);
812
+ const expr2 = funcStr.replace(/\^/g, '**').replace(/x/g, `(${point - h})`);
813
+
814
+ try {
815
+ const f1 = eval(expr1);
816
+ const f2 = eval(expr2);
817
+ const derivative = (f1 - f2) / (2 * h);
818
+
819
+ // Symbolic derivative for simple cases
820
+ let symbolicDerivative = "Unknown";
821
+ if (funcStr.includes('x^2')) {
822
+ symbolicDerivative = funcStr.replace('x^2', '2*x');
823
+ } else if (funcStr.includes('x^3')) {
824
+ symbolicDerivative = funcStr.replace('x^3', '3*x^2');
825
+ } else if (funcStr === 'x') {
826
+ symbolicDerivative = '1';
827
+ } else if (!isNaN(parseFloat(funcStr))) {
828
+ symbolicDerivative = '0';
829
+ }
830
+
831
+ document.getElementById('derivativeResult').textContent = symbolicDerivative;
832
+ document.getElementById('derivativeAtPoint').textContent = derivative.toFixed(4);
833
+
834
+ // Update integral result
835
+ let integral = "Unknown";
836
+ if (funcStr.includes('x^2')) {
837
+ integral = funcStr.replace('x^2', '(1/3)*x^3');
838
+ } else if (funcStr === 'x') {
839
+ integral = '(1/2)*x^2';
840
+ } else if (!isNaN(parseFloat(funcStr))) {
841
+ integral = `${funcStr}*x`;
842
+ }
843
+ document.getElementById('integralResult').textContent = `${integral} + C`;
844
+
845
+ // Update chart
846
+ updateChart();
847
+ } catch (e) {
848
+ document.getElementById('derivativeResult').textContent = "Error";
849
+ document.getElementById('derivativeAtPoint').textContent = "Error";
850
+ document.getElementById('integralResult').textContent = "Error";
851
+ }
852
+ }
853
+
854
+ function calculateIntegral() {
855
+ // This function is now integrated into calculateDerivative
856
+ calculateDerivative();
857
+ }
858
+
859
+ function clearCalculus() {
860
+ document.getElementById('functionInput').value = 'x^2';
861
+ document.getElementById('pointInput').value = '2';
862
+ document.getElementById('derivativeResult').textContent = '2*x';
863
+ document.getElementById('pointValue').textContent = '2';
864
+ document.getElementById('derivativeAtPoint').textContent = '4';
865
+ document.getElementById('integralResult').textContent = '(1/3)*x^3 + C';
866
+ updateChart();
867
+ }
868
  // History functions
869
  function addToHistory(expression, result) {
870
  const historyItem = {