Results 1 to 11 of 11
Thread: Calculus & java
 03282011, 02:05 PM #1Member
 Join Date
 Mar 2011
 Posts
 27
 Rep Power
 0
Calculus & java
yesterday i was study calculus " An Increasing / decreasing seq " ..
in calculus we solve these problems as following :
given (an), and find if sequences is Inc or Dec.
we have to find an+1 first .
then divide an+1/an ..
if the result < 1 so it is decreasing , otherwise it is increasing.
i solved many problems with many cases .. factorial , exponential functions .. etc . I said to myself .. if i have program to make my life easier and give me the answer directly that will save my time .. and my effort :rolleyes: .
i thought about this program starting by solving simple problem for example:
an = n .
an+1 = n+1
an+1/an = n+1/n .... n+1 > n .. so that it is increasing .
if i could write this program .. then i believe i can write complex cases
such as factorial , ..etc.
Now ,
input is an .. which user give to the program .
of course ! input maybe n or n+1 , n+2 .. etc.
but what i have focus on is replace each n by( n+1 ) !
this what i did until now ! , because i started programming using java script , and am just little bit confused about java .. please tell me does until
now am on the line .. give me yours advice to improve myself ..
am stack little bit .. but i believe i can complete this program.
Java Code:public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); System.out.println("Please Enter the an"); String an = input.nextLine(); System.out.println("your an is"+"["+an+"]"); int a = an.length(); System.out.println("the an length is"+"["+a+"]"); char []arr=an.toCharArray(); String aa = "n+1"; for(int index=0;index<arr.length;index++) { System.out.println("the CharArray is"+"["+arr[index]+"]"); } for(int i =0;i<arr.length;i++) if(an.charAt(i)=='n') { System.out.println("the new is"+"["+aa+"]"); } else { System.out.println("the new is"+"FAILED "); } }
Last edited by SHE; 03282011 at 03:29 PM.
 03282011, 02:29 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,831
 Blog Entries
 7
 Rep Power
 21
Can you please tell us in ordinary words what you're trying to do because I (we) understand zilch from your code ...
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 03282011, 02:55 PM #3Member
 Join Date
 Mar 2011
 Posts
 27
 Rep Power
 0
Program determine whether the sequence Increasing or Decreasing.
The input of program is : an.
[an] should contain (n) !
steps to determine whether this input is Increasing or Decreasing.
1. count an+1 .. by replace each n by n+1 ..
for example if an = n+1 , an+1 = (n+1)+1= n+2 and so on.
.
.
step two :
divide an+1/an ..
case one if the result < 1 // it is decreasing , other wise .. it is increasing.
 03282011, 03:04 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,831
 Blog Entries
 7
 Rep Power
 21
cenosillicaphobia: the fear for an empty beer glass
 03282011, 03:20 PM #5Member
 Join Date
 Mar 2011
 Posts
 27
 Rep Power
 0
let's take an = n + 1
an+1 will be = n+2
an+1/an = n+2/n+1 .. you can notice now ( an+1 )is greater than (an ) .. so it is increasing , this simplest case for these problems.
.
.
so now i faced two things :
1. the input will be String .. , if i replace each n by n+1 , after the operation how i can sum the numbers when the input is String , so that i thought about the char Array !
but i still can't sum the numbers and keep the n . ( this the first thing which i want to know .. and i think after that i can do the step two)
I hope you understand my point ^^"
 03282011, 03:50 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,831
 Blog Entries
 7
 Rep Power
 21
 03282011, 04:13 PM #7Member
 Join Date
 Mar 2011
 Posts
 27
 Rep Power
 0
i already putted this example :
let's take an = n + 1
an+1 will be = n+2
an+1/an = n+2/n+1
the input (n+3)/(n+2)
because these operations about sequences so we need to know what is after this input.
so that program will replace each n by (n+1).. the output which display to the user
should be as following n+4/n+3 .. this first step.
let's see my code :
Java Code:Scanner input = new Scanner(System.in); System.out.println("Please Enter the an"); String an = input.nextLine(); System.out.println("your is"+"["+an+"]"); [COLOR="Red"]// HERE THE PROGRAM WILL TAKE THE INPUT FROM USER. [/COLOR] char []arr=an.toCharArray(); [COLOR="Red"]// Now i putted the input to char array to be able to me change each n . [/COLOR] String aa = "n+1"; // an+1 is constant.
I MADE THESE CHANGES WHICH ARE WRONG , THAT IN RED ....
Java Code:Scanner input = new Scanner(System.in); System.out.println("Please Enter the an"); String an = input.nextLine(); System.out.println("your is"+"["+an+"]"); char []arr=an.toCharArray(); [COLOR="Red"]char A ='n'+'1';[/COLOR] System.out.println(A); for(int index=0;index<arr.length;index++) { System.out.println("the CharArray is"+"["+arr[index]+"]"); } for(int i =0;i<arr.length;i++) if(an.charAt(i)=='n') { char a = an.charAt(i); [COLOR="Red"] an.replace(a,A);[/COLOR] } else { System.out.println("the new is"+"?????"); } }
 03282011, 04:33 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,831
 Blog Entries
 7
 Rep Power
 21
Hold it for a minute; if your input is "(n+3)/(n+2)" and you replace each "n" by "n+1", you get "(n+1+3)/(n+1+2)"; it takes a lot more logic to find out that you want "(n+4)/(n+3)". And what if the input was "n/2"; if you replace each "n" by "n+1" you get "n+1/2" which is totally wrong ...
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 03282011, 04:45 PM #9Member
 Join Date
 Mar 2011
 Posts
 27
 Rep Power
 0
yeah this logic is what am asking about .. which i have no idea about how i can implement it in java.
if input n/2 .. output will n+1/2 .. !
step two .. i have to divide n+1/2 / n/2 .. and then i have do the multiplication .. will get
n+1/2 * 2/n & the final answer is .. n+1/n .. after if statement will find that
input is increasing.
so that i said what i want to try it now is simple case if it is work then i have to think
in other cases ... there is sin , cos , factorial .. etc.
 03282011, 04:54 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,831
 Blog Entries
 7
 Rep Power
 21
So after 10 replies or so we can conclude that you want to solve this problem symbolically and you have no idea how to do it, right? I can tell you that a textual substitution, as your tried, is never going to work. You need to know the 'depth structure' of the expression, so you need to parse the textual representation and generate an AST (Abstract Syntax Tree) from it. Only then you can substitute the expression n+1 for the subexpression n.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 03282011, 05:26 PM #11Member
 Join Date
 Mar 2011
 Posts
 27
 Rep Power
 0
Similar Threads

For the people who love Calculus
By tim in forum Forum LobbyReplies: 9Last Post: 12012009, 01:53 PM 
How to best design a calculus solver
By xcallmejudasx in forum New To JavaReplies: 4Last Post: 06252009, 05:27 AM
Bookmarks