An Introduction to MathCAD You can always find a solution !! When things go wrong f( x) 5 x 79 x 4 f1( x) d f( x) dx f1( x) 5x 4 2180 x 3 316 x 133764 x 2 6540 x 3 2 3098448 x 16039296 267528 x 3098448 56 34 roots f( x) 0 solve x 78 12 9 turnings f1( x) 0 solve x “Result is too large to Display” MathCAD #6 dpl 2001 2 Can we fix it ? Yes we can !! f( x) 1.0 x 79 x 5 2180 x 4 f1( x) d f( x) dx f1( x) 5.0 x 4 133764 x 3 316 x 3 2 6540 x 2 3098448 x 16039296 267528 x 3098448 56. 34. roots f( x) 0 solve x 78. 12. 9. 27.033163438232028292 turnings f1( x) 0 solve x 10.481803779012762137 31.678249774297679276 69.036717442947111153 Force solver to work with real numbers MathCAD #6 dpl 2001 3 What if its still broken ? f( x) 5 x 79 x 4 f1( x) d f( x) dx f1( x) 5x 4 2180 x 3 316 x 133764 x 2 6540 x 3 2 3098448 x 267528 x 16039296 3098448 56 34 roots f( x) 0 solve x 78 12 9 27.033163438446261373 turnings f1( x) 0.0001 solve x 10.481803778652327208 31.678249774081398831 69.036717443017189749 Risky !! – OK in this case MathCAD #6 dpl 2001 4 Still can’t solve it ? Use polyroots to find roots Use coeffs keyword on solver to get coefficients of x 3098448 267528 cfsx f1( x) coeffs x 6540 316 5 27.033 polyroots ( cfsx) 10.482 31.678 69.037 MathCAD #6 dpl 2001 5 Still can’t solve it ? Graph shows maxima & minima => there must be solutions Try guessing & using roots(f1(x),x) MathCAD #6 dpl 2001 6 Solving ODEs numerically Produce numeric solution to system of ODEs. Must have initial conditions Manipulate equations Use one of several different solvers Produces matrix of solutions MathCAD #6 dpl 2001 7 First order linear ODE #1 Radioactive decay, Newton’s law of cooling etc d A dt kA A is amount of material temperature difference etc k is rate constant MathCAD #6 dpl 2001 8 First order linear ODE #2 Define initial conditions as a vector 1st order so only 1 element in vector Can’t use units in ODE solver Call vector ‘ic’ Element 0 = A at t=0 ic0 100 MathCAD #6 dpl 2001 9 First order linear ODE #3 Now define ODE & manipulate for mathCAD d A dt k kA 0.1 D( t A ) kA MathCAD #6 dpl 2001 10 First order linear ODE #4 Now define range of solution t0 0 t1 5 N 1000 Start time Finish Time Number of Points And solve using rkfixed Soln rkfixed( ic t0 t1 N D) Creates matrix ‘Soln’ containing solution MathCAD #6 dpl 2001 11 First order linear ODE #5 The Solution Matrix 0 0 1 0 100 1 5·10 -3 99.501 2 0.01 99.005 3 0.015 98.511 4 0.02 0.025 97.531 6 0.03 97.045 Soln 7 0.035 96.561 8 0.04 96.079 0.045 Column 1 holds A values 98.02 5 9 Column 0 holds t values 1 row of matrix per timestep (0..N) 95.6 10 0.05 95.123 11 0.055 94.649 12 0.06 94.176 13 0.065 93.707 14 0.07 93.239 15 0.075 92.774 0.08 92.312 MathCAD #6 dpl 2001 12 First order linear ODE #6 Plot the results Use M<> to extract columns Use subscripting to extract rows i 0 N 100 1 Soln i 50 0 0 2 4 6 0 Soln i MathCAD #6 dpl 2001 13 Second Order ODE Same steps as for first order Slightly less obvious manipulation Replace ODE by system of 1st orders Can use symbolic solver to formulate equations MathCAD #6 dpl 2001 14 Second Order ODE Example #1 Damped SHM – LCR circuit – Damped Pendulum d L i dt i R q C 0 Substitute for i & divide through by L d q i dt Gives: d 2 q d t2 Rd L dt q MathCAD #6 dpl 2001 1 q 0 LC 15 Second Order ODE Example #2 d 2 q d t2 Rd L dt 1 q 0 LC q rewrite equation (by hand) substituting 2 d q q2 d t2 d q q1 dt q q0 Gives manipulated equation: q2 R L q1 MathCAD #6 dpl 2001 1 q0 0 L C 16 Second Order ODE Example #3 R q2 L Use q2 R L q1 1 q0 0 L C solver to solve for q2 q1 1 q0 0 solve q2 L C ( R q1 C q0) ( L C ) Now ready to create D(t,q) function MathCAD #6 dpl 2001 17 Second Order ODE Example #4 Equations d q q1 dt to create D(t,q) d 2 q q2 d t2 ( R q1 C q0) ( L C ) 2 row vector to hold D(t,q) Change subscripts for suffixes q1 D( t q ) R q1 C d q dt q0 ( L C ) MathCAD #6 dpl 2001 d 2 q d t2 18 Second Order ODE Example #5 Specify initial conditions For second order need 2 elements in ic vector ic 1 Charge (q) at t=0 0 Current (dq/dt) at t=0 Also need to specify constants in D(t,q) equations Watch for lack of units !! L 10 2 C 10 3 R MathCAD #6 dpl 2001 1 19 Second Order ODE Example #6 Define times and number of points as before Call rkfixed to solve t1 0.1 Soln t0 0 N 1000 rkfixed( ic 0 t1 N D) Matrix Soln filled with solution points MathCAD #6 dpl 2001 20 Second Order ODE Example #7 • The solution matrix 0 0 1 2 3 4 5 6 Soln 7 8 9 10 11 12 13 14 15 1 0 1·10 2·10 3·10 4·10 5·10 6·10 7·10 8·10 9·10 1·10 1.1·10 1.2·10 1.3·10 1.4·10 1.5·10 1.6·10 -4 -4 -4 -4 -4 -4 -4 -4 -4 -3 -3 -3 -3 -3 -3 -3 2 1 1 0.998 0.996 0.992 0.988 0.982 0.976 0.969 0.961 0.952 0.942 0.932 0.92 0.908 0.895 0.881 0 -9.949 -19.788 -29.51 -39.106 -48.568 -57.887 -67.055 -76.066 -84.912 -93.585 -102.078 -110.386 -118.501 -126.417 -134.129 -141.631 Column 0 holds t values Column 1 holds q values Column 2 holds dq/dt values MathCAD #6 dpl 2001 21 Second Order ODE Example #8 Graphing Time the solution 0 Soln Charge 1 Soln 1 0.5 Charge 0 0.5 1 0 0.05 0.1 Time MathCAD #6 dpl 2001 22 Surely nothing can go wrong ? Solution relies on numeric integration which divides timestep up into smaller chunks for integration If system is changing much faster than timestep, solution will fail Clues to look for: – “found a number >10^307” – Singularity at tfinish MathCAD #6 dpl 2001 23 Can we fix it ? Check time constants Try more point in solution (N) Try smaller interval Use a different solver – ‘Stiff’ systems give problems – Special solvers for stiff systems – See Quicksheet & help system MathCAD #6 dpl 2001 24
© Copyright 2026 Paperzz