Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added derivative calculator #435

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 239 additions & 0 deletions funwithphysics/src/Components/Integration/Topic/Calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useParams } from "react-router";
import "./Calculator.css";
import { Form, Button } from "react-bootstrap";
import limits1 from "../../../Images/limits1.png";
import derivative1 from "../../../Images/derivative1.png";

function Calculator() {
let { topic } = useParams();
Expand Down Expand Up @@ -134,6 +135,47 @@ function Calculator() {
" Hence,the limit of function at x -> 2 is -0.75.",
],
},
{
topic: "Derivative",
details: `The derivative of a function of a real variable measures the sensitivity to change of the function value with respect to a change in its argument. `,
formula: "f'(x)=(Δx→0) lim Δy/Δx = dy/dx ",
process: [
"The derivative of a function of a single variable at a chosen input value, when it exists, is the slope of the tangent line to the graph of the function at that point. ... The process of finding a derivative is called differentiation. ",
<br />,
],
example1: [
<b>1)</b>,
"Find the derivative of the function sin(x^2) at x -> a where a is 3.",
<br />,
<b>Solution: </b>,

"Finding the derivative of sin(x^2) we get , ",
<br />,
"f'(x) = 2*x*cos(x^2) ",
<br />,
"Substituting x = 3 , we get slope as 6*cos(9rad)",
<br />,
<br />,
" Computing the value we get the slope as ",
<b> -5.43 </b>,
],
example2: [
<b>2)</b>,
"Find the derivative of the function 1/x at x -> a where a is 0.",
<br />,
<b>Solution: </b>,

"Finding the derivative of 1/x we get , ",
<br />,
"f'(x) = -1/x^2 ",
<br />,
"Substituting x = 0, we get slope as 1/0.",
<br />,
<br />,
" Computing the value we get the slope as ",
<b> Infinity</b>,
],
},
];

const page = Topics.filter((data) => data.topic === topic);
Expand Down Expand Up @@ -414,6 +456,199 @@ function Calculator() {
</>
);
};

const Derivative = () => {
const [parameter, setparameter] = useState("");
const [func, setfunc] = useState("");

const [result, setResult] = useState(null);
const reset = () => {
setparameter("");
setfunc("");
setResult(null);
};

function compute() {
let f = func;

f = f.split("^").join("**");

f = f.split("sin").join("Math.sin");

f = f.split("cos").join("Math.cos");

f = f.split("tan").join("Math.tan");

f = f.split("cosec").join("Math.cosec");

f = f.split("sec").join("Math.sec");

f = f.split("cot").join("Math.cot");

f = f.split("e").join("Math.exp");

f = f.split("log").join("Math.log");
console.log(f);

const exp = f.split("x").join(parseFloat(parameter));
console.log(exp);
return eval(exp);
}
function computewithvalue(value) {
let f = func;
let v = value;
if (value>-1 && value<1){
v='0';
}
f = f.split("^").join("**");

f = f.split("sin").join("Math.sin");

f = f.split("cos").join("Math.cos");

f = f.split("tan").join("Math.tan");

f = f.split("cosec").join("Math.cosec");

f = f.split("sec").join("Math.sec");

f = f.split("cot").join("Math.cot");

f = f.split("e").join("Math.exp");

f = f.split("log").join("Math.log");
console.log(f,v);
const exp = f.split("x").join(v);
console.log(exp);
return eval(exp);
}

function slope(x1, y1, x2, y2) {
return (y1 - y2) / (x1 - x2);
}

const calculatederiv = () => {
var at = compute();
if (Math.abs(at) == Infinity || at !== at) {
setResult("Infinity");
return ""
}
var y1 = compute();
var x0 = parameter - 0.000000000000001;
var y0 = computewithvalue(x0);
var x2 = parameter + 0.000000000000001;
var y2 = computewithvalue(x2);
var slope1 = slope(x0, y0, parameter, y1);
var slope2 = slope(parameter, y1, x2, y2);

if (x0-parameter === 0 || x2-parameter ===0){
setResult("Infinity")
return ""
}
if (Math.abs(slope1 - slope2) > 0.1) {
setResult(slope1);
return ""
}
setResult(slope1)

return ""
}
return (
<>
<Form>
<Form.Group className="mb-4" controlId="text">
<Form.Text className="text">
<strong> To find the derivative of function</strong>

<br />
</Form.Text>
</Form.Group>

<Form.Group className="mb-4" controlId="text">
<Form.Text className="text">
<strong>
[Trigonometric values should be enclosed in brackets (Eg:-
sin(x^2))]
</strong>
<br />
</Form.Text>
<div className="straight_calc">
<form name="first">
<div className="group">
<div className="coordinate">
<label>Limit at x =</label>
<input
name="parameter"
type="text"
className="easynumeric"
onChange={(e) => setparameter(e.target.value)}
value={parameter}
/>
</div>

<div className="coordinate">
<label>
Function
<br />
[w.r.t x]
</label>
<input
name="function"
type="text"
className="easynumeric"
onChange={(e) => setfunc(e.target.value)}
value={func}
/>
</div>
</div>
</form>
</div>
</Form.Group>

<Form.Group className="mb-4">
<Form.Control
readOnly
type="number"
placeholder={result === null ? "Result" : result + " "}
/>
</Form.Group>
</Form>
<div className="button-custom-grp mb-5">
<Button variant="primary" onClick={calculatederiv}>
Calculate
</Button>
&nbsp;&nbsp;&nbsp;
<Button variant="dark" onClick={() => reset()} type="reset">
Reset
</Button>
</div>
<div className="formula-table mb-4">
<table className="formulae">
<thead>
<section className="m-2">
<tr>
<h3>Derivative</h3>
</tr>
</section>

<tr>
<th className="row-1 row-ID "> Limit</th>
<th className="row-2 row-name ">Formula</th>
</tr>
</thead>
<tbody>
<tr>
<td>Derivative of f(x) at x = a</td>
<td>
<img src={derivative1} alt="derivative" />
</td>
</tr>
</tbody>
</table>
</div>
</>
);
};
//adding the calculators together
function calC(key) {
let currentCall;
Expand All @@ -423,6 +658,10 @@ function Calculator() {
break;
case "Limits":
currentCall = Limit();
break;
case "Derivative":
currentCall = Derivative();
break;
default:
break;
}
Expand Down
6 changes: 6 additions & 0 deletions funwithphysics/src/Components/Integration/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export default function Integration() {
formula: "",
process: "",
},
{
topic: "Derivative",
details: ``,
formula: "",
process: "",
},
];

return (
Expand Down
Binary file added funwithphysics/src/Images/derivative1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.