Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having problems implementing mathematical equations in programming

I am implementing an algorithm for school and am having problems understanding how a definite integral is represented in programming. For example I know that the Summation equation can be implemented as the following example:
enter image description here

assuming y=f(x)

if(x==0){
    y=x+1;
}else{
    for(int i = 0; i < n; i++){
        y = y + (x - 1);
    }
}

How would I then represent a numerical integral, example:
enter image description here
The equations planted here may not make mathematical sense but my objective is to implement similar equations in c# for a school programming project that I have to do in which I have to implement an algorithm which contains integrals. I have been reading that there are numerical methods to solve definite integrals such as simpson rule; would I have to use such methods to implement the equation or can an integral be represented in programming such as a loop or something of that sort?

like image 231
user1327159 Avatar asked Jun 08 '12 12:06

user1327159


People also ask

Why is it important to know the mathematical equation in programming?

Those who understand equations behind programming languages will easily comprehend their structure, which will come in handy later when some actual programming starts to take shape.

What is a mathematical programming problem?

Mathematical programming refers to mathematical models used to solve problems such as decision problems. The terms are meant to contrast with computer programming which solves such problems by implementing algorithms which may be designed specifically for a given problem.

How is mathematics involved in programming?

This field of mathematics is also utilized in a wide array of programming areas such as making visuals or graphs, simulations, coding-in applications, problem-solving applications, analysis and design of algorithms, and making statistic solvers.

How can I improve my maths in programming?

A really great resource is a book called Competitive Programming 3 the book is great overall, and most importantly the Math chapter in it. The authors have really gathered a lot of Math-related problems and if you solve just a few you will definitely improve your Math and Problem Solving skills.


1 Answers

It depends on what you are trying to do. If this was a specific implementation you could simply integrate the formula x-1 becomes (x^2)/2 - x and then return the max value minus the minimum value.

Alternatively it can be implemented as an estimation choosing an appropriate step size for dx.

decimal dx=0.1;

if(x==0){  
    y=x+1;  // could just return y=1
}else{  
    decimal tempY=0;
    for(decimal i = 3; i <= 20; i+=dx){  
        tempY += (i - 1);  
    }  
    // Either return tempY as decimal or
    y= Convert.ToInt32(tempY);
}  
like image 80
Bob Vale Avatar answered Sep 30 '22 15:09

Bob Vale