Answer the question
In order to leave comments, you need to log in
Calculate integral in JS?
Hello everyone, there was a task to calculate the integral of a given function. I tried to write the code, but I'm not sure that the solution is correct, something tells me that the formula and the solution in general are not written correctly. Could you see where the errors are? Thanks in advance.
function integral(expenssion, a, b){
if(!expenssion || !b&&!a) return 0;
if (a==b) return 0;
var x,y1,y2,n,length,dx, s=0;
y1=Math.min(a,b); //Если левый предел больше правого
b=Math.max(a,b); //Если правый предел меньше левого
a=y1;
n=100;
if (length>2) n=Math.round(100*Math.log(length+1));
dx=length/n; //Это вычисление длины элементарного отрезка
x=a; //Начальное значение Х в выражении
y1=eval(expenssion);
x=a+dx;
y2=eval(expenssion);
s=((Math.pow(x,n)*Math.cos(n*(Math.PI/3)))*dx)/2;
for (i=2; i<=n; i++){
y1=y2;
x=x+dx;
y2=eval(expenssion);
s+=((Math.pow(x,n)*Math.cos(n*(Math.PI/3)))*dx)/2;
}
return s
}
integral(3, 6, 1);
Answer the question
In order to leave comments, you need to log in
Everything is bad.
The variable length
is not defined.
The call assumes that the first parameter is the code of the expression, not a number.
Your task is to write an approximate calculation of def. integral of a particular function?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question