<Text-field layout="Heading 1" style="Heading 1">Chapter 8: Programming -- logic, loops, and procedures</Text-field>
<Text-field layout="Heading 2" style="Heading 2">Logic</Text-field>
<Text-field layout="Heading 3" style="Heading 3"> if-then-elif-then-else-end if (fi)</Text-field>restart;Define NiMvLSUiZkc2IyUieEctJSpQSUVDRVdJU0VHNiU3JCIiIjIiIiFGJzckRi4vRidGLjckLCRGLCEiIjJGJ0Yuf:= x-> if x<0 then -1 elif x=0 then 0 else 1 end if;Check:f(-.5);f(0);f(.5);Define NiMvLSUiZ0c2IyUieEctJSpQSUVDRVdJU0VHNiQ3JComIiIiRi0sJkYtRi0qJEYnIiIjRi0hIiIyRiciIiE3JComJSRjb3NHRi1GJ0YtJSpvdGhlcndpc2VH fi = end ifg:= x-> if x<0 then 1/(1+x^2) else cos(x) fi;Check:g(-1.5); g(2.5); # worksg(Pi); # doesn't work: Pi is not a numberg(evalf(Pi)); # worksPlot:plot( g(x), x=-10..10 ); # fails: x is not a numberplot( g, -10..10 ); # procedures are plotted in operator notationDerivatives:diff(g(x),x); # doesn't work: x is not a numberD(g); D(g)(1.); # worksIntegration:int( g(x), x=-2..2 ); # doesn't work, can't be fixed.Conclusion: the if-then...end if structure is not very convenient for defining functions: use the piecewise construct instead.
<Text-field layout="Heading 3" style="Heading 3">Boolean expressions</Text-field> < less than <= less than or equal > greater than >= greater than or equal = equal <> not equal and, or, not conjunctionsevalb(7>5); evalb(3=4); evalb(3<>4);
<Text-field layout="Heading 3" style="Heading 3">piecewise</Text-field>g:= x-> piecewise( x<0, 1/(1+x^2), x>=0, cos(x) );plot( g(x), x=-5..5 );int( g(x), x=-5..5 );diff(g(x),x);
<Text-field layout="Heading 2" style="Heading 2">Loops</Text-field>
<Text-field layout="Heading 3" style="Heading 3">for-from-by-to-do-end-do</Text-field>answer:=0;for i from 1 to 100 do answer:= answer + i^2;end do:answer;The above do loop is equivalent to the sumi:='i': sum(i^2,i=1..100);Sum of only odd numbers:answer:=0;for i from 1 by 2 to 99 do answer:=answer + i^2;od: # same as end do:answer;
<Text-field layout="Heading 3" style="Heading 3">for-from-while-do (<Font executable="false" family="Times New Roman" foreground="[0,0,0]" italic="true" size="12" style="_cstyle260" underline="false"> Successive Substitution Loop )</Font></Text-field>Solving the equation NiMvKiYlJGNvc0ciIiIlInhHRiZGJw== :Do exactly 20 iterations:x:=5.;for i from 1 to 20 do x:= cos(x);od;Check answer:[x,cos(x)];Do until error is less then NiMpIiM1LCQiIikhIiI=:x:=5.; xold:=0;for i from 1 while abs(x-xold)>1e-8 dotempx:= cos(x);xold:= x;x:= tempx;end do: # this ':' suppresses all outputsx; # answer Check answer:[x,cos(x)];
<Text-field layout="Heading 3" style="Heading 3">break</Text-field>for i from 1 to 100 do if i=20 then break end if;od:i;
<Text-field layout="Heading 2" style="Heading 2">Procedures</Text-field>
<Text-field layout="Heading 3" style="Heading 3">example 1</Text-field>restart;f:= proc(x,y) local a,b,z; # declare local variables global d; # declare global variables a:=3.; b:=17.; z:= a*b*x*y*d; # value of the last expression is returnedend proc;Check result:f(1,2); # d not yet assignedd:=31.; f(1,2);
<Text-field layout="Heading 3" style="Heading 3">example 2</Text-field>restart;cosdeg:= proc(theta) local phi,answer; phi:= theta*Pi/180.; answer:= cos(phi);end; # same as end procNow test ityy:= cosdeg(45.);
<Text-field layout="Heading 3" style="Heading 3">Debug</Text-field>To debug example 2 above, usetrace(cosdeg);then execute it like thiscosdeg(45);And then when it is working right and you don't want to see all of this stuff every time you use it typeuntrace(cosdeg);
<Text-field layout="Heading 2" style="Heading 2">Example: plotting results from fsolve</Text-field>The following procedure solves NiMvLSUkY29zRzYjJSJ4RyomJSJrRyIiIkYnRio= where k varies from 0 up to 20. restart;Declare F to be a procedure F:= proc(k)Declare variables that will only be used inside the procedurelocal s,x;Use fsolve to solve the equation using the value of k passed in through proc(k)s:= fsolve( cos(x)-k*x, x, 0..2 );Returnend proc;Try a few valuesF(1); F(2);Make a plot (use operator notation--the form plot(F(k),k=0..20) doesn't work)plot( F, 0..20);