problems with integration - Printable Version +- HP Forums ( https://www.hpmuseum.org/forum)+-- Forum: HP Calculators (and very old HP Computers) ( /forum-3.html)+--- Forum: HP Prime ( /forum-5.html)+--- Thread: problems with integration ( /thread-2507.html) |

RE: problems with integration - Han - 12-08-2014 02:19 AM
(12-07-2014 12:07 PM)resolved Wrote: how would I assign a function, the integral of a prior declared function. I just can't seem to get the HP Prime to work in this regard. To create a function, use one of the syntaxes below: 1. f(x):=a*x^2+b*x+c; // creates a function \( f(x) = ax^2+bx+c\) OR 2. f:=(x)->(a*x^2+b*x+c); // creates a function \( f(x)=ax^2+bx+c \) Please note that f is now a CAS function, and f(x) is the evaluation of the CAS function f at an input value of x. Thus the result of f(x) is a CAS expression (not to be confused with the mathematical function of the same syntax). When you integrate using int(expr, var), the expr argument is a CAS expression. We must keep those concepts straight. To create a function that is the integral of a previously defined function: 1a. int(f(x),x); 1b. g(x):= tap the screen and copy the result OR (for a single command) 2. g:=unapply(int(f(x),x), x); Now typing g(x) will give the result: 1/3*a*x^3+1/2*b*x^2+c*x. The unapply(expr, var) will evaluate the expression expr and then create a function (with dummy variable var). In the CAS world, something like g(x):=int(f(x),x) is not well-defined due to ambiguity in the substitution priority. It's obvious to a human what is likely the intended result, but not to a computer program. Quote:I am getting to the point to believe that I can NOT use the HP Prime to use the double integration method to solve for deflection in beams. I am hoping someone will prove me wrong........ You can, so long as you understand that the typical integral (the one implemented in the HP Prime and pretty much any CAS) is _NOT_ the same as the integral operator used on \(n\)-th order ramp functions and similar discontinuous functions (D.F.) commonly used in beam problems. For \( n\ge 0 \) and \( x_0 \le a \), \[ \int_{x_0}^x (t-a)^n\ dt = \frac{1}{n+1}(x-a)^{n+1} -\frac{1}{n+1}(x_0-a)^{n+1}\] whereas \[ \int_{x_0}^x \langle t-a\rangle^n\ dt = \frac{1}{n} \langle x-a \rangle^{n+1} \] The HP properly handles ramp functions of order \( n\ge 2\) defined as a piecewise function since its integration algorithm resembles the reverse of the chain rule which coincides with the your D.F. integration. (You will also get the warning about integrating piecewise functions.) It's the ramp function and other D.F. whose exponent is less than 2 that will not integrate as you want because the CAS uses the "regular" integrals and not the ones relevant to D.F. So there are a few ways about doing the double integration technique. 1. Use piecewise functions for all ramp functions of order \( n\ge 2\) and leave out the lower-order ramp functions to do by hand. Then just add them in after your double integral. OR 2. You can implement the following program that will return the proper "antiderivative" of a D.F. Code: `#cas` MINT(a,n,d) computes the \(d\)-th antiderivative of \( \langle x-a \rangle^n \). For example, MINT(3,1,2) is equivalent \[ \int \int \langle x-3 \rangle\ dx\ dx = \int \frac{1}{2} \langle x-3 \rangle^2\ dx = \frac{1}{6} \langle x-3\rangle^3 \] except the result from MINT(3,1,2) is a CAS function equivalent to \[ (x) \rightarrow \left(\frac{1}{6} \langle x-3 \rangle^3 \right) \] You can use \( d=0 \) if you don't want to integrate and just want the \(n\)-th order ramp function. I didn't implement the doublet function. I'll leave that as an exercise to the reader. (It can be defined as a 5-piece piecewise function. The three non-zero components are just lines.) Solving: \[ y'' = -24 + 18 \langle -9 + x\rangle + 3 \langle -9 + x\rangle^2 + 21 \langle -3 + x\rangle - 3 \langle -3 + x\rangle ^2 \] f(x):=-24; // in general, this is the non D.F. component; note the functional notation y1:=unapply( int(f(x),x)+c1 , x) + 18*MINT(9,1,1) + 3*MINT(9,2,1) + 21*MINT(3,1,1) -3*MINT(3,2,1); y0:=unapply( int(y1(x),x)+c2 , x); // here regular integration is ok since we no longer have ramp functions of order 1 or lower solve({y0(3)=0,y0(9)=0},{c1,c2}); OR f(x):=-24; y:=unapply(int(int(f(x),x)+c1,x)+c2,x) + 18*MINT(9,1,2) + 3*MINT(9,2,2) + 21*MINT(3,1,2) -3*MINT(3,2,2); solve({y(3)=0,y(9)=0},{c1,c2}); Basically, use the HP Prime's int() function to handle the non-D.F. portion (including the constants c1 and c2. As for the D.F. part, use the MINT() program. If you plan to work with doublet functions and D.F.'s of more negative powers \( n < -2\) you will need to adjust the code for MINT() to handle those cases. It's really just a "shortcut" to some simple integrals which you can easily do by hand with piecewise functions anyway. Lastly, by having the results be functions, you can then use "substitution" in the solve command by simply evaluating the functions and setting the result equal to zero: y(3)=0 for example. EDIT: There are probably other ways to do this. RE: problems with integration - Thomas Klemm - 12-08-2014 08:50 AM
(12-08-2014 02:19 AM)Han Wrote: \[ \int_{x_0}^x (x-a)^n\ dx \]You mustn't use the same variable \(x\) both as the upper limit and control variable of the integration. That would be similar to write in C: Code: `for (x = 0; x < x; x++) {` \[ \int_{x_0}^x (t-a)^n\ dt \] RE: problems with integration - Han - 12-08-2014 03:57 PM
(12-08-2014 08:50 AM)Thomas Klemm Wrote: You mustn't use the same variable \(x\) both as the upper limit and control variable of the integration. Doh! Thank you for the correction. I've gone back to make appropriate edits. RE: problems with integration - resolved - 01-03-2015 11:45 AM
Thanks Han for all the hard work, sorry for the delay, had finals and the holidays. Your program was my first program to enter into the Prime. Unfortunately it didn't go well. It took me about an hour, partly due to the difficulty in reading the keys to input your code. Upon finishing, I pressed the soft key "check" and got a syntax error on line 0 "#cas" I needed a rest so pressed ESC to exit programing. Usually, ESC will save what I have entered, at least in Notes it will, but it didn't in programs so I lost everything. I will have to start all over again, but before I do, I want to understand that I type into the Prime the program just as it is written in the paragraph entitled Code: ???? including "#cas" and "#end" ???? RE: problems with integration - danielmewes - 01-03-2015 08:26 PM
Quote:including "#cas" and "#end" ???? I believe this syntax was added with the latest firmware update. Previously there was a checkbox instead to make a program a CAS program (they use a different syntax and environment than non-CAS programs). From the release notes of firmware version 2014 12 03 (6975): Quote:Add ability to mix and edit CAS functions inline as part of larger programs - #cas to start a block, and #end to end it. You can update the firmware of your Prime by connecting it to a PC and using the Connectivity Kit's check for updates function. PS: While you're at it: An easier option for entering the program is using the Virtual Calculator emulator that can be downloaded from Code: `ftp://ftp.hp.com/pub/calculators/Prime/` RE: problems with integration - resolved - 01-06-2015 01:55 AM
Life is not easy when you don't know what you are doing. Thanks danielmewes for your comments -- I downloaded all three programs, so I have the new connectivity kit, the new virtual calculator and the new firmware loaded in my handheld calculator version 6975. In the virtual calculator, I opened programs, new, named the new program "BeamDeflection", erased the existing code, copied and pasted Han's code and got an syntax error on line 2 "MINT(a,n,d):=" I tried renaming MINT to BeamDeflection, but didn't work. I tried renaming the program to MINT, but didn't work. I deleted the programs and started all over naming the program MINT and pasting in Han's code and got the same "Syntax error in program line 2" so what am I doing wrong here???? RE: problems with integration - danielmewes - 01-06-2015 04:04 AM
I can't update my Virtual Calculator at the moment, but this works for me with the slightly older version: * Create a new program called MINT, check the CAS box! * Delete everything * Copy Han's program, but without the #cas and #end (since we've checked CAS when creating the program, those aren't necessary anymore). The last line should be END; and the first line should be MINT(a,n,d):= * Click Check. Should show "No errors in the program". * To test, press the CAS button, enter MINT(1,1,1). You should get a function (x) -> {.... RE: problems with integration - Han - 01-06-2015 01:09 PM
(01-06-2015 01:55 AM)resolved Wrote: Life is not easy when you don't know what you are doing. 1. Make sure you have the most recent version of the virtual calculator or the most recent firmware for the hardware. 2. Create a new program, delete the template that is created, and copy the code exactly as shown. It does not matter if you check CAS or not since the #cas and #end declaratives will ensure that the final result is a CAS program. 3. The easiest way to create this program is the copy the code into the virtual calculator (create a new program in the virtual calculator, clear the template that gets created, and just paste the code into the program editor on the virtual calculator). Once all is working on the virtual calculator, connect your calculator to your PC with the virtual calculator running, and send from virtual calculator to your actual calculator. There is no reason to have to hand-type any code on the actual calculator. Edit: Can you post a screenshot of the program editor with the first few lines of code that you pasted into the virtual calculator? RE: problems with integration - resolved - 01-07-2015 03:57 AM
how do you insert a screenshot?? "Insert image" wants a URL "http://" [img]Libraries\Pictures\Capture.png[/img] I'll try inserting the harddrive address between "[img]" I'll try "Add Attachment" I still get a syntax error RE: problems with integration - Han - 01-07-2015 05:11 AM
The only suspicion I have left is that there are some whitespaces that are not recognized by the emulator. In your screenshots, there are spaces at the start of each line whereas when I copy/paste the code I do not have spaces at the start of each line. I've attached a zip file with the actual program. You can drag it into the connectivity kit (left pane, bottom section) and send from there, or copy it to your HP Prime folder for the emulator. RE: problems with integration - Marcus von Cube - 01-07-2015 07:27 AM
(01-07-2015 03:57 AM)resolved Wrote: how do you insert a screenshot?? "Insert image" wants a URL "http://" Do you really wonder that a picture from your hard drive will not show up? (01-07-2015 03:57 AM)resolved Wrote: I'll try "Add Attachment" Much better! After you have added a picture as an attachment, you are offered a button "Insert into post" which should take care of the correct syntax for embedding the picture. Have a try in the test forum! RE: problems with integration - Snorre - 01-07-2015 08:57 AM
Hello resolved, Han had the right idea: (01-07-2015 05:11 AM)Han Wrote: The only suspicion I have left is that there are some whitespaces that are not recognized by the emulator. In your screenshots, there are spaces at the start of each line [...]It turns out the Prime doesn't like spaces before the "#end" directive, i.e. Code: `#cas` Code: ` #cas` RE: problems with integration - Claudio L. - 01-07-2015 03:48 PM
I don't have a Prime, but here's my take on the problem itself: You have several piecewise functions (your load functions), call them w1, w2, w3..., separated at points a, b, c... You want to integrate each piecewise function a number of times to obtain shear, moment, rotation and deflection diagrams. Any CAS will always give you the "family" of curves, and you must supply the boundary conditions for each piecewise function to determine the value of integration constants. The CAS may or may not add the constants explicitly (in most cases they don't). Your steps to integrate this properly are (same as doing it by hand): - Compute the indefinite integral (integrate as many times as needed)
- Add the correction constants to the expression, as a polynomial. If you integrated 'n' times, you'll need to add C1*x^(n-1)+C2*x^(n-2)+...+Cn. For example, if you integrate 3 times, the first integral will have +C1, which integrated will become +C1'*x, and a new C2 has to be added, and the next step you'll end up with C1''*X^2+C2'*x+C3.
- Repeat the two previous steps for all your piecewise components
- Now you have a system of equations with 'n' unknowns (the Cn constants), where you need to apply your boundary and continuity conditions. If you do deflection (let's call the resulting functions D1, D2, D3...), you can write D1(0)=0, D2(a)=D1(a), D3(b)=D2(b)... This basically does continuity of deflections at all points in the beam (and zero deflection at left support, assuming x=0 is the support). You'll need additional equations, so you can write some for continuity of the rotations, or points of zero moment, etc. (bear in mind that you'll have to derive the deflection functions with the added constants to obtain rotation and moment equations that have the SAME constants you want to solve for). When you have enough equations, you solve for all your constants and will have a complete family of equations that work the way they are supposed to be.
The key in all this is to obtain the proper boundary conditions to get the right constants. The solution proposed by Han to do multiple integration in one shot will only help you get a nice and clean expression at the first step of my list above, but you can't forget that you have to add a constant at each integration step, which ends up in a polynomial expression in x. His solution quickly dismisses those constants by applying an implicit boundary condition Fn(a)=0 at each integration step (resulting in Cn=-Fn(a)=0). This does not apply to your problem. In the beam these constants will exist and not necessarily be zero, it depends heavily on the configuration of the beam and loads. Here's a full example using two constant distributed loads w1 and w2, that change at point 'a' in a simply supported beam: w1(x)= w1 (it's a constant) from 0 to a w2(x)= w2 (constant) from a to b (b=right support) Step 1: integrate 4 times to get deflection: V1(x)=w1*x ( +C1 ) --> Shears M1(x)=w1*x^2/2 (+C1*x+C2 ) --> Moments r1(x)=w1*x^3/6 ( +C1/2*x^2 + C2*x + C3 ) --> Rotations d1(x)=w1*x^4/24 ( +C1/6*x^3 + C2/2*x^2 + C3*x + C4 ) --> Deflections We can do the same integration for the other function: [[ skipping the most obvious ]] d2(x)=w2*x^4/24 ( +C5/6*x^3 + C6/2*x^2 + C7*x + C8 ) --> Deflections Side note: Han's program would give you just the main term: d1(x)=w1*x^4/24 d2(x)=w2*(x-a)^4/24 Also notice that d2() in this case would be "shifted by a" in the x direction, so the constants would end up with a different numeric value, but the end function would be the same after applying the correct boundary conditions. While it may look better, it doesn't really save you any work versus the more generic approach I'm following here. Especially because at a given point 'a' in the middle of a beam, in general neither deflection, rotation, moment or shears are zero, so all your constants will have a numeric value. Now we have 8 unknown constants, here are the boundary conditions: Deflections are zero at supports: d1(0)=0 --> C4=0 d2(b)=0 Zero moments at supports: M1(0)=0 --> C2=0 M2(b)=0 Material continuity at the point where load changes: d1(a)=d2(a) r1(a)=r2(a) Equilibrium: V1(0)+V2(b)=0 Continuity of shear at point a: V1(a)=V2(a) 8 equations with 8 constants: 2 are immediately eliminated (C2 and C4 =0) 2 are easy to find from the last 2 shear equations (C1 and C5) This was a simple example, I don't need to state that these boundary conditions only apply to this very simple example and need to be adapted for every problem. The Prime CAN and WILL help you do all these individual steps, which involve not only the symbolic integration, but also valuing your functions at various points for the boundary conditions AND also solving the linear system of equations to get your constants. You just have to follow a very methodical solution path to the problem and it will get you there. EDIT: If your instructor is asking for the integration constants, he is asking for trouble because those constants will change depending on how you integrate (not just a CAS may use different techniques, but each student could use a different technique too). But all final polynomial expressions (including the numeric constants) will have the exact same coefficients after taking them to the same canonical form. This could help you "check" if your constants are correct and prove your instructor that you are right. Claudio PS: My apologies to everybody for the excessive length of this post. RE: problems with integration - resolved - 01-08-2015 12:40 AM
"Do you really wonder that a picture from your hard drive will not show up?" Marcus, I am not a programmer. I don't know what you guys can or can not do. I read language literally, not as a secrete code. If the tag on the icon says "insert image" I would think it could grab an image off a server just as easy as it could grab an image off my hard drive. To me "Add Attachment" would be used for adding documentation or a file or an image at the end of ones post. If "insert image" is not what it does then the tag should be changed to something understandable in plain English, that is the point of a tag is to help not confuse. Thanks Snorre - the one white space in front of #end was the problem. RE: problems with integration - Tim Wessman - 01-08-2015 03:53 PM
(01-08-2015 12:40 AM)resolved Wrote: Thanks Snorre - the one white space in front of #end was the problem. I've put that in the list to look at. I was pretty sure that shouldn't have made a difference. Thanks! RE: problems with integration - resolved - 01-09-2015 04:13 AM
Thanks Claudio for your kind input, I have read your post twice, but I will need more time to absorb it all because this subject was taught to me differently. Below is a sample problem I was given: [attachment=1418] This is how I solve the problem in Mathematica. Solving for the two supports: NSolve[{(*Ma*) 0 == -25 + 7*2*(1) + 7*(2) + 14*2*(3) + 11 - dv*(7) + 7*2*(8),(*Fv*) 0 == -7*2 - 7 - 14*2 - 7*2 + av + dv}, {av, dv}] {{av -> 33., dv -> 30.}} Setting the problem up: y21 = -25 + 33 x - (7 x*(x/2) - 7 (x - 2) (x - 2)/2) - 7 (x - 2) - (14 (x - 2) (x - 2)/2 - 14 (x - 4) (x - 4)/2) + 11 (x - 4)^0 + 30 (x - 7) - 7 (x - 7) (x - 7)/2 Mathematica simplified y21 to: y22 = -14 + 30 (-7 + x) - 7/2 (-7 + x)^2 + 7 (-4 + x)^2 - 7 (-2 + x) - 7/2 (-2 + x)^2 + 33 x - (7 x^2)/2 but I manually broke the two couples apart as shown below y22 = -25 + 11 (x - 4)^0 + 30 (-7 + x) - 7/2 (-7 + x)^2 + 7 (-4 + x)^2 - 7 (-2 + x) - 7/2 (-2 + x)^2 + 33 x - (7 x^2)/2 I then integrated manually to get: y11 = -25 x + 11 (x - 4) + 30 (-7 + x)^2/2 - 7/2 (-7 + x)^3/3 + 7 (-4 + x)^3/3 - 7 (-2 + x)^2/2 - 7/2 (-2 + x)^3/3 + 33 x^2/2 - 7 /2 x^3/3 Mathematica simplified y11 to (see below) and I added c1: y12 = 15 (-7 + x)^2 - 7/6 (-7 + x)^3 + 11 (-4 + x) + 7/3 (-4 + x)^3 - 7/2 (-2 + x)^2 - 7/6 (-2 + x)^3 - 25 x + (33 x^2)/2 - (7 x^3)/6 + c1 I then manually integrated a second time to get (below) and added c2: y01 = 15 (-7 + x)^3/3 - 7/6 (-7 + x)^4/4 + 11 (-4 + x)^2/2 + 7/3 (-4 + x)^4/4 - 7/2 (-2 + x)^3/3 - 7/6 (-2 + x)^4/4 - 25 x^2/2 + 33/2 x^3/3 - 7/6 x^4/4 + c1 x + c2 Mathematica simplified to get: y02 = c1 x + c2 + 5 (-7 + x)^3 - 7/24 (-7 + x)^4 + 11/2 (-4 + x)^2 + 7/12 (-4 + x)^4 - 7/6 (-2 + x)^3 - 7/24 (-2 + x)^4 - (25 x^2)/2 + ( 11 x^3)/2 - (7 x^4)/24 I then used "Max" instead of piecewise to solve for c1 and c2: y[x_] = c1 x + c2 + 5 Max[0, (-7 + x)]^3 - 7/24 Max[0, (-7 + x)]^4 + 11/2 Max[0, (-4 + x)]^2 + 7/12 Max[0, (-4 + x)]^4 - 7/6 Max[0, (-2 + x)]^3 - 7/24 Max[0, (-2 + x)]^4 - (25 x^2)/2 + ( 11 x^3)/2 - (7 x^4)/24; NSolve[{y[0] == 0, y[7] == 0}, {c1, c2}] {{c1 -> -48.9048, c2 -> 0.}} I then substituted in for c1 and was able to create a plot of the beam deflection yf[x_] := 5 Max[0, (-7 + x)]^3 - 7/24 Max[0, (-7 + x)]^4 + 11/2 Max[0, (-4 + x)]^2 + 7/12 Max[0, (-4 + x)]^4 - 7/6 Max[0, (-2 + x)]^3 - 7/24 Max[0, (-2 + x)]^4 - (25 x^2)/2 + ( 11 x^3)/2 - (7 x^4)/24 - 48.904761904761905` x; Plot[yf[x], {x, 0, 9}] [attachment=1419] Now HP Prime's turn using Han's program MINT I set up the initial equation as shown below: y1:=−25*MINT(0,0,2)+11*MINT(4,0,2)+30*MINT(7,1,2)-(7/2)*MINT(7,2,2)+7*MINT(4,2,2)-7*MINT(2,1,2)-(7/2)*MINT(2,2,2)+33*MINT(0,1,2)-(7/2)*MINT(0,2,2) The program worked and I got: (x)->-25*CASE IF 0>x THEN 0 END IF x≥0 THEN 1/2*x^2 END END+11*CASE IF 4>x THEN 0 END IF x≥4 THEN 1/2*(x-4)^2 END END+30*CASE IF 7>x THEN 0 END IF x≥7 THEN 1/6*(x-7)^3 END END-7/2*CASE IF 7>x THEN 0 END IF x≥7 THEN 1/12*(x-7)^4 END END+7*CASE IF 4>x THEN 0 END IF x≥4 THEN 1/12*(x-4)^4 END END-7*CASE IF 2>x THEN 0 END IF x≥2 THEN 1/6*(x-2)^3 END END-7/2*CASE IF 2>x THEN 0 END IF x≥2 THEN 1/12*(x-2)^4 END END+33*CASE IF 0>x THEN 0 END IF x≥0 THEN 1/6*x^3 END END-7/2*CASE IF 0>x THEN 0 END IF x≥0 THEN 1/12*x^4 END END I added c1*x + c2 to y1 y2:=y1 + c1*x + c2 and solved for c1 and c2 fsolve({y2(0)=0,y2(7)=0},{c1,c2}) and got the result: [−48.9047619048,0.] cool, much faster than by hand with less chance for error I then created a new function y3:=y1 -48.9047619048 x (how do you extract just the -48.904... out of the matrix???, had to type it in) I checked my memory in CAS variables and found y1, y2, y3 Great now I have an equation that will give me the deflection of the beam at any point on the beam.. so lets try at the end of the beam, 9 meters from point A y3(9) damn -- soooo close, application crashed and I lost all my variables y1,y2 and y3 [attachment=1420] so what did I do wrong this time???? RE: problems with integration - danielmewes - 01-09-2015 04:52 AM
(01-09-2015 04:13 AM)resolved Wrote: so what did I do wrong this time???? Looks like you have found a bug. That's annoying :-( I tried your example on the real calculator and it crashes as well, requiring a hard reset via the reset button on the back. The odd thing is that I can evaluate y1 without any problems. Only when evaluating y3 I'm getting the crash. Edit: Defining `y3:= y1 - 1072/21` also works, but `y3:= y1 - x` crashes upon evaluating `y3(9)`. (Tip: If you want to make sure that you don't lose data on a crash, you can turn the calculator off and back on occasionally to persist its memory state) RE: problems with integration - danielmewes - 01-09-2015 05:00 AM
Running `simplify(y1 - x)` instantly restarts the calculator. Edit: Here's the full definition of `y1` to reproduce the bug: Code:
RE: problems with integration - parisse - 01-09-2015 07:21 AM
y1-x is invalid since y1 is a function and x an expression, but it should not crash of course. Can you "simplify" the bug, I mean find a smaller expression where you get a crash? This would help me because efficient debugging is done inside Xcas and CASE/IF... structures are HP specific. RE: problems with integration - danielmewes - 01-09-2015 07:41 AM
@parisse Your explanation about the fact that we were subtracing an expression from a function lead me to this simple example which freezes my calculator as well: Code:
Evaluating y2(0) freezes the Prime and crashes Xcas with a segmentation fault. Also note that y2 is printed as if it was a valid function `(x) -> x-x`, which makes it look like this sort of thing was a valid function definition. |