Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2019-03-13T22:43:44+00:00 → 2019-05-27T11:29:25+00:00
<language>Javascript</language> | |
| − | === Fakultät === |
| − | <code id="5b915b8493c39"> |
| − | function fakultaet(n) { |
| − | var produkt = 1; |
| − | for (var i = 1;i < n;i++) { |
| − | produkt = produkt * i; |
| − | } |
| − | return produkt; |
| + | =Eigenschaften und graphische Darstellung quadratischer Funktionen= |
| + | ==Herausfinden der Parameter== |
| + | <code id="5c950c31a1103"> |
| + | function getabc(gleichung) { |
| + | gleichung = gleichung.replace(/(?:y=|f\(x\)=)/,''); |
| + | if(gleichung.startsWith('+')) gleichung = gleichung.replace('+',''); |
| + | var parameter = { |
| + | 'a': '', |
| + | 'b': '', |
| + | 'c': '' |
| + | } |
| + | |
| + | let check = false, |
| + | digits = 0, |
| + | posa = gleichung.indexOf('x²') - 1; |
| + | |
| + | if(posa == -2) return; |
| + | else switch(gleichung[posa]){ |
| + | case undefined: parameter.a = 1; gleichung = gleichung.replace(`x²`,''); break; |
| + | case '+': parameter.a = 1; gleichung = gleichung.replace(`+x²`,''); break; |
| + | case '-': parameter.a = -1; gleichung = gleichung.replace(`-x²`,''); break; |
| + | default: while(!check){ |
| + | if(gleichung[posa - digits] * 1 || gleichung[posa - digits] == '0' || gleichung[posa - digits] == '.'){ |
| + | parameter.a += gleichung[posa - digits]; |
| + | digits++; |
| + | } |
| + | else{ |
| + | if(gleichung[posa - digits] == '-') parameter.a += '-'; |
| + | check = true; |
| + | parameter.a = parameter.a.split('').reverse().join('') * 1; |
| + | gleichung = gleichung.replace(`${parameter.a}x²`,''); |
| + | } |
| + | } |
| + | } |
| + | if(gleichung.startsWith('+')) gleichung = gleichung.replace('+',''); |
| + | |
| + | check = false; |
| + | digits = 0; |
| + | let posb = gleichung.indexOf('x') - 1; |
| + | |
| + | if(posb == -2) parameter.b = 0; |
| + | else switch(gleichung[posb]){ |
| + | case undefined: parameter.b = 1;gleichung = gleichung.replace(`x`,'');break; |
| + | case '+': parameter.b = 1;gleichung = gleichung.replace(`+x`,'');break; |
| + | case '-': parameter.b = -1;gleichung = gleichung.replace(`-x`,'');break; |
| + | default: while(!check){ |
| + | if(gleichung[posb - digits] * 1 || gleichung[posb - digits] == '0' || gleichung[posb - digits] == '.'){ |
| + | parameter.b += gleichung[posb - digits]; |
| + | digits++; |
| + | } |
| + | else{ |
| + | if(gleichung[posb - digits] == '-') parameter.b += '-'; |
| + | check = true; |
| + | parameter.b = parameter.b.split('').reverse().join('') * 1; |
| + | gleichung = gleichung.replace(`${parameter.b}x`,''); |
| + | } |
| + | } |
| + | } |
| + | if(gleichung.startsWith('+')) gleichung = gleichung.replace('+',''); |
| + | if(gleichung.endsWith('+')) gleichung = gleichung.substring(0, gleichung.length - 1); |
| + | |
| + | parameter.c = gleichung * 1; |
| + | return(parameter); |
} | |
</code> | |
| − | <run id="5b915b8493cc0">fakultaet(ZAHL);</run> |
| − | === Wurzel === |
| − | <code id="5b9f982833d5b"> |
| − | function heron(z, x) { |
| − | var x0 = x; |
| − | do { |
| − | x = x0; |
| − | x0 = (x + z / x) / 2; |
| − | } |
| + | ==Ausgabe der Nullstellen und des Scheitelpunktes== |
| + | <code id="5cdbb5849b632"> |
| + | function infos(parameter){ |
| + | if(parameter.a == 0){ |
| + | //mn-Form: |
| + | let m = parameter.b, |
| + | n = parameter.c; |
| − | while(x0 != x); |
| − | return x0; |
| − | } |
| − | </code> |
| − | === Linie === |
| − | <code id="5b915df63ab8a"> |
| − | function linie(Ax, Ay, Bx, By) { |
| − | //weitere Anweisungen |
| − | return funktion; |
| − | } |
| − | </code> |
| − | |
| − | A('''2'''|'''1''') B('''5'''|'''7''') -> linie'''(2, 1, 5, 7)''' |
| − | |
| − | <run id="5b915df63abff">linie(2, 1, 5, 7);</run> |
| − | === Scheitelpunkt (x²) === |
| − | <code id="5b92be1f22a8b"> |
| − | function parabel(A1, A2, S1, S2) { |
| − | //weitere Anweisungen |
| − | return funktion; |
| − | } |
| − | </code> |
| − | |
| − | y='''a'''x²+'''b'''x+'''c''' -> scheitelpunkt'''(-2, 4, 0)''' |
| − | |
| − | <run id="5b92be1f22b3c">scheitelpunkt(-2, 4, 0)</run> |
| − | === Die Endversion: === |
| − | <code id="5c89571c8a184"> |
| − | function infos(term){ |
| − | //a,b,c herausfinden: |
| − | var a = term.substring(0 , term.indexOf('x²')) * 1, |
| − | b = term.substring(term.indexOf('x²') + 2, term.indexOf('x', term.indexOf('x²') + 2)) * 1, |
| − | c = term.substring(term.indexOf('x', term.indexOf('x²') + 2) + 1) * 1, |
| − | p = b / a, |
| − | q = c / a; |
| − | |
| − | //Nullstelle: |
| − | var x1 = 0, |
| − | x2 = 0; |
| − | if((p * p / 4) - q < 0){ |
| + | //Nullstelle: |
| + | var x0 = 0; |
| + | if(m==0) |
| + | if(n==0) |
| + | x0 = 'unendlich viele NST'; |
| + | else |
| + | x0 = 'keine NST'; |
| + | else |
| + | x0 = Number(- n / m); |
| + | |
| + | //Ausgabe: |
| + | console.log(`<i>Nullstelle:</i> <b>${x0}</b>`); |
| + | } |
| + | else{ |
| + | //pq-Form: |
| + | let p = parameter.b / parameter.a, |
| + | q = parameter.c / parameter.a; |
| + | |
| + | //Nullstelle: |
| + | var x1 = 0, |
| + | x2 = 0; |
| + | if((p * p / 4) - q < 0){ |
x1 = 'nicht definiert'; | |
x2 = 'nicht definiert'; | |
| − | } |
| − | else if((p * p / 4) - q == 0){ |
| + | } |
| + | else if((p * p / 4) - q == 0){ |
x1 = Number((- (p / 2)).toFixed(3)); | |
x2 = 'nicht definiert'; | |
| − | } |
| − | else if((p * p / 4) - q > 0){ |
| + | } |
| + | else if((p * p / 4) - q > 0){ |
x1 = Number((- (p / 2) + Math.sqrt((p * p / 4) - q)).toFixed(3)); | |
x2 = Number((- (p / 2) - Math.sqrt((p * p / 4) - q)).toFixed(3)); | |
| − | } |
| − | else return console.error('Given function contains too few numbers!'); |
| + | } |
| − | //Scheitelpunkt: |
| − | var x = Number((- (p / 2)).toFixed(3)), |
| − | y = Number((- (p * p / 4) + q).toFixed(3)); |
| − | |
| − | //Ausgabe: |
| − | console.log(`Nullstelle 1: ${x1}`); |
| − | console.log(`Nullstelle 2: ${x2}`); |
| − | console.log(`Scheitelpunkt: S(${x}|${y})`); |
| + | //Scheitelpunkt: |
| + | var x = Number((- (p / 2)).toFixed(3)), |
| + | y = Number((parameter.a * (- (p * p / 4) + q)).toFixed(3)); |
| + | |
| + | //Ausgabe: |
| + | console.log(`<i>Nullstelle 1:</i> <b>${x1}</b><br> <i>Nullstelle 2:</i> <b>${x2}</b><br> <i>Scheitelpunkt:</i> <b>S(${x}|${y})</b>`); |
| + | } |
} | |
| + | </code> |
| − | var xAchse = 400 / 2, yAchse = 400 / 2; |
| − | |
| − | function koordinatensystem(canvas, turtle){ |
| − | canvas.clear(); |
| − | turtle.hide(); |
| − | turtle.move(0, xAchse); |
| + | ==Grafische Darstellung== |
| + | <code id="5c950c31a11e2"> |
| + | function grafik(canvas, turtle, parameter){ |
| + | canvas.clear(); |
| + | turtle.hide(); |
| + | turtle.move(0, xAchse); |
| + | turtle.right(90); |
| + | turtle.forward(400); |
| + | turtle.move(yAchse, 0); |
| + | console.log('<font color="#ffffff"> Hier könnte Ihre Werbung stehen!</font>') |
| + | turtle.right(90); |
| + | turtle.forward(400); |
| + | for(var i = 1; i <= 19; i++){ |
| + | turtle.move(20 * i, xAchse - 5); |
| + | turtle.forward(10); |
| + | turtle.left(90); |
| + | turtle.move(yAchse - 5, 20 * i); |
| + | turtle.forward(10); |
turtle.right(90); | |
| − | turtle.forward(400); |
| − | turtle.move(yAchse, 0); |
| − | turtle.right(90); |
| − | turtle.forward(400); |
| − | for(var i = 1; i <= 19; i++){ |
| − | turtle.move(20 * i, xAchse - 5); |
| − | turtle.forward(10); |
| − | turtle.left(90); |
| − | turtle.move(yAchse - 5, 20 * i); |
| − | turtle.forward(10); |
| − | turtle.right(90); |
| − | } |
| + | } |
| + | for(var x = -10; x <= 10; x += 0.0025){ |
| + | posx = 20 * x + yAchse; |
| + | posy = -20 * (parameter.a * x * x + parameter.b * x + parameter.c) + xAchse; |
| + | turtle.move(posx, posy); |
| + | turtle.forward(0.01); |
| + | } |
} | |
| − | |
| − | function parabel(turtle, term){ |
| − | var a = term.substring(0 , term.indexOf('x²')) * 1, |
| − | b = term.substring(term.indexOf('x²') + 2, term.indexOf('x', term.indexOf('x²') + 2)) * 1, |
| − | c = term.substring(term.indexOf('x', term.indexOf('x²') + 2) + 1) * 1; |
| − | |
| − | for(var x = -10; x <= 10; x += 0.0025){ |
| − | posx = 20 * x + yAchse; |
| − | posy = -20 * (a * x * x + b * x + c) + xAchse; |
| − | turtle.move(posx, posy); |
| − | turtle.forward(0.01) |
| − | } |
| − | turtle.hide(); |
| − | } |
</code> | |
| + | ==Ergebnis am "Front-End"== |
<canvas w="400" h="400"></canvas> | |
| − | <run id=5c89571c8a184> |
| − | var term = "1x²-2x+1"; |
| − | infos(term); |
| − | koordinatensystem(canvas1, turtle1); |
| − | parabel(turtle1, term); |
| + | <run id="5cdbb5849b6bd" htmloutput> |
| + | const gl = '0.5x²+2x-3'; |
| + | let pm = getabc(gl); |
| + | if(!pm) console.log('<i>Die angegebene Funktion ist keine quadratische Funktion!</i>'); |
| + | else { |
| + | infos(pm); |
| + | var xAchse = 400 / 2, yAchse = 400 / 2; |
| + | grafik(canvas1, turtle1, pm); |
| + | } |
</run> |