/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/ /* [ Created with wxMaxima version 17.10.0 ] */ /* [wxMaxima: title start ] Symbolic Computing Exercise package X [wxMaxima: title end ] */ /* [wxMaxima: comment start ] Author: X E-mail: x.x@x.x [wxMaxima: comment end ] */ /* [wxMaxima: comment start ] Many of the problems below require reading (wx)Maxima's documentation. If only a command name is given, it is up to you to figure out how to use it. In this package you are free to choose the exercises. Please update the following two numbers if appropriate: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ skipped_exercises:0 $ /*number of exercises in previous packages that you should have completed but could not*/ penalties:0 $ /*number of times you have missed a deadline or tried to return the final exercises too many times*/ /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] The number of exercises you need to do in package X is: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ 5+skipped_exercises+5*penalties; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Execute the cell above and complete at least that number of exercises below. A couple of extra will be a safe bet. PLEASE LIST THE NUMBER OF EXERCISES DONE BELOW: Section 1, Complex numbers: 0 Section 2, Random numbers: 0 Section 3, More calculus: 0 Section 4, Miscellaneous: 0 Section 5, LaTeX: 0 [wxMaxima: comment end ] */ /* [wxMaxima: section start ] Complex Numbers [wxMaxima: section end ] */ /* [wxMaxima: input start ] */ kill(all); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Complex numbers are number of the form a+bi, where a and b are real numbers and i is the imaginary unit. Imaginary unit satisfies equation i^2 = -1. Maxima syntax for the imaginary unit is %i. Search from the manual Data type and Structures -> Numbers -> Complex numbers and Mathematical functios -> Function for complex numbers. [wxMaxima: comment end ] */ /* [wxMaxima: comment start ] The usual representation of a complex number is a+bi and it's called Cartesian representation (rect-form in Maxima). The numbers a and b are its real and imaginary parts respectively (realpart and imagpart in Maxima). The complex conjugate of a+bi is the number a-bi (conjugate in Maxima). For example, [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ realpart((2+3*%i)^2); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ imagpart((2+3*%i)^2); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] That is (2+3i)^2 = -5 + 12i. [wxMaxima: comment end ] */ /* [wxMaxima: comment start ] Complex numbers have other representations as well. Polar form (polarform in Maxima) of a complex number z is a representation z = r*cos(w)+i*r*sin(w) = r*e^(i*w). Here r is called the complex absolute value of z and it is the distance of z from the origin (cabs in Maxima). The angle w is the complex argument of z (carg in Maxima). It denotes the angle between x axis and the ray through z starting from the origin (-pi < w <= pi). For example, [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ cabs((2+3*%i)^2); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ carg((2+3*%i)^2),numer; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] That is (2+3*i)^2 is approximately equal to 13*e^(2*i). [wxMaxima: comment end ] */ /* [wxMaxima: subsect start ] Exercises [wxMaxima: subsect end ] */ /* [wxMaxima: subsubsect start ] [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] The matrix C:matrix([-3,1+%i],[1-%i,-2]) is called hermitean because the complex conjugate of its transpose is C itself. What properties should the eigenvectors and eigenvalues of such a matrix have? (You can check Wikipedia or any source to find this out.) Check whether these properties hold. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] What are the eigenvalues of the matrix R:matrix([1,1],[-1,1])? Express these complex numbers in polar form. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: section start ] Random Numbers [wxMaxima: section end ] */ /* [wxMaxima: input start ] */ kill(all); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ load("eigen")$ /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Maxima has a function called random. This takes a positive integer n as an input and returns a (pseudo) random integer between 0 and n-1. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ random(10); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Random numbers can be used to generate random examples for computations. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ randomList:makelist(random(n),n,10,15); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ v:covect(randomList); /* [wxMaxima: input end ] */ /* [wxMaxima: subsect start ] Exercises [wxMaxima: subsect end ] */ /* [wxMaxima: subsubsect start ] [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] a) How do you get a random number between 0-99? How about between 1-100? [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] b) How do you get a random real nummber on interval (0,1)? How about on interval (-1,1)? [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] c) Create a 5x5-matrix of random intergers from interval [-5,5]. Compute its determinant. Commands makelist and apply might be useful. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: section start ] More Calculus [wxMaxima: section end ] */ /* [wxMaxima: input start ] */ kill(all); /* [wxMaxima: input end ] */ /* [wxMaxima: subsect start ] Partial Derivatives [wxMaxima: subsect end ] */ /* [wxMaxima: comment start ] If a function f depends on more than one variable its derivatives are called partial derivatives and denoted by df/dx, df/dy, etc (see equation (3) in the PDF). Similarly derivatives of second order are denoted by d^2f/dx^2, d^2f/dxdy, etc (equation (4) in the PDF). (the first one means differentiate f twice with respect to x, the second means that first f is differentiated with respect to y and then with respect to x). Often a symbol called doo is used in place of d. We already know that the command diff computes derivatives of a function. Actually, diff computes partial derivatives of a function. For example, [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ diff(cos(x^2-y^2),x); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ expr:log(x^2+y^2); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ xDoubleDer:diff(expr,x,2); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ yDoubleDer:diff(expr,y,2); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] A curious phenomenon appears in this example: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ ratsimp(xDoubleDer+yDoubleDer); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] This means that the function is harmonic (outside the origin, as the result is actually invalid at that point). There is a bit more about this in the exercises. [wxMaxima: comment end ] */ /* [wxMaxima: subsect start ] Differential Equations [wxMaxima: subsect end ] */ /* [wxMaxima: input start ] */ kill(all); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Let us try solving following differential equation: d^2y/dx^2 + 2y = sin(x), where y is a function of x. We will use Maxima's command ode2 to solve this. Command ode2 wants an equation as one of the inputs, so first we define this equation in Maxima: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ eq: 'diff(y,x,2) + 2*y = sin(x); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Pay attention to the apostrophe in front of command diff. This tells Maxima not to execute the diff command immidiatelly. Try what happens if the apostrophe is dropped from the equation to see why it is needed. Now we are ready to solve the equation: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ GenSolution:ode2(eq,y,x); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Sometimes we want a solution that has specified initial conditions for the solution function y (and maybe its derivatives). This type of a problem is called initial value problem. Let us solve an initial value problem d^2y/dx^2 + 2y = sin(x), y'(0) = 1, y(0) = 1. This is now easy, since we already have the general solution to this differential equation. To impose initial restrictions in Maxima we use command ic2. This takes initial values as arguments in addition to a general solution to the equation: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ IbSolution:ic2(GenSolution,x=0,y=1,'diff(y,x)=1); /* [wxMaxima: input end ] */ /* [wxMaxima: subsect start ] Exercises [wxMaxima: subsect end ] */ /* [wxMaxima: input start ] */ kill(all); /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Prove that df/dt = d^2f/dx^2, when f(x,t) = t^(-1/2)*e^(-x^2/(4*t)). It might be easiest to study the difference of the two derivatives. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Define following functions: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ laplacian(f,V):=sum( diff(f, V[j], 2), j,1,length(V)); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ heat(f,t,V):=diff(f,t) - laplacian(f,V); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] We saw in 6.1. that if f(x,y) = log(x^2+y^2), then d^2f/dx^2 + d^2f/dy^2 = 0. This can now be proved easily with the laplacian function: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ LaplLog:laplacian(log(x^2+y^2),[x,y]); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] To see that this is actually zero we need to simplify: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ ratsimp(LaplLog); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] It's not the case that laplacian of any function is zero (not all functions are harmonic): [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ laplacian(sin(x)+cos(y),[x,y]); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ laplacian(%e^(x+y+z),[x,y,z]); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Also, the previous exercise can be veryfied with the function heat. a) Compute d^2f/dx^2 + d^2f/dy^2, when f(x,y)=arctan(y/x). [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] b) Compute d^2f/dx^2 + d^2f/dy^2 + d^2f/dz^2, when f(x,y,z)=1/sqrt(x^2+y^2+z^2). [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] c) Compute df/dt - ( d^2f/dx^2 + d^2f/dy^2 ), when f(x,y,t)=t^(-1)*e^(-(x^2+y^2)/(4*t)). [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Names of the functions defined above reflect that these functions are differential operators called the Laplace operator and the heat operator: f --> d^2f/dx_1^1 + d^2f/dx_2^2 + ... + d^2f/dx_n^2 (Laplace operator; Delta) f --> df/dt - Delta(f) (Heat operator). (equations (5) and (6) in the PDF) [wxMaxima: comment end ] */ /* [wxMaxima: subsubsect start ] [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Define in Maxima the function v: R^2 --> R^2, v(x,y) = (sin(x*y),y^2). You can use lists, column vectors or row vectors (lists are propably the most convenient for this). Compute the divergence of this vector field. Divergence of a vector field F: R^2 --> R^2, F(x,y) = (F_1(x,y),F_2(x,y)) is defined to be div(F) := dF_1/dx + dF_2/dy (equation (7) in the PDF). [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: section start ] Miscellaneous [wxMaxima: section end ] */ /* [wxMaxima: subsubsect start ] Anonymous functions 1 [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Some functions, such as apply, require a function as an input. Often one names a function for this reason (such as f(x):=x^2;), but it isn't always convenient to name every single object. Another option is to use anonymous functions. The command lambda is used for creating anonymous functions. Compute the value of function t --> t^3 - t at t=4 using anonyous an function. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Anonymous functions 2 [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Continying previous exercise let us define [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ monkey:banana^3; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Use this object and lambda-function to define function g such that g(t) = t^3. Don't write g(t):=t^3, but use lambda. (Try evaluation inside lambda.) [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Applying function to a list [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Let following list be given [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ lst:makelist(i*%pi/4,i,0,10); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Compute value of x*sin(x) at every point in the list. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Matrix action on square [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Draw the square with vertices (1,1), (1,2), (2,2), and (2,1) in the plane. Connect the dots and make the square red. Operate on these vectors by the matrix with rows (2,3) and (-1,-1). Draw the new "square" in green into the same picture. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Reading from text file [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Maxima can read a definition of a function from a text file. There is no need to write many similar lines of code to do something that repeats. It's enough to load a function from a file. Open an ordinary text editor and save a file containing following commands. Name your file as "crit_points_p.mac": crit_points_p():=( p:readonly("Give a polynomial p(x):"), dp:diff(p,x), p_roots:map(lambda([z],part(z,2)),sort(realroots(dp))), if length(p_roots) = 0 then error("Error: The derivative does not have real roots"), p_values:map(lambda([y],ev(p,x=y)),p_roots), min_root:first(p_roots), max_root:last(p_roots), min_value: first(sort(p_values)), max_value: last(sort(p_values)), wxdraw2d( xrange=[min_root-1,max_root+1], xaxis=true, yrange=[min_value-1,max_value+1], color=blue, explicit(p,x,min_root-1,max_root+1), color=black, points_joined=false, point_type=circle, point_size=1, points(p_roots,p_values) ) )$ The file will contain a definition of function named crit_points_p. Now you can import the function by choosing File -> Batch file ... and opening the file you just created. Try executing [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ crit_points_p(); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Examine the definition of the function and explain how it works and what it does? You don't have to separetely return the file you just created. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Some integrals [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Compute integrals of tan(x) on both intervals [0,pi] and [0,2pi]. Plot the graph of tan(x) on interval [0,2pi]. Comment on the results. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Harmonic Oscillator [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] From basic physics courses you might remember the weight oscillating in the end of a spring. Recall that the movement of the oscillator is sine like. Define this as location x depending on time t that is x:A*sin(omega*t). Here A is the amplitude and omega is the angle speed. Let the mass of the weight be m and the spring constant be k. In this case -kx=ma according to Newton's second law, where a is the acceleration of the weight. Name this expression in Maxima. Remember that the acceleration is the second time derivative of the location x. Simplify your expression with factor command and name the simplified expression. From this you should see that m*omega^2-k has to be zero. Collect this information with part (part(...,1,1) should do). You should get m*omega^2-k, name this expression. Use solve command to solve omega, when it's known that m*omega^2-k is equal to zero. Now we have found oscillation frequency as a function of mass and spring constant using Maxima. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Antiderivatives [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Draw a plot of the function x^2*sin(x) and three different antiderivatives of it. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Roots and plotting [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Consider function f(x) = x^3 - 5*x^2 + 2*x + 1. Choose a root x0 of this function (use part). Plot into a same picture the graph of f and the point (x0,f(x0))=(x0,0). Load graphics package draw and Newton-package (load(draw) and load(mnewton)). [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Newton's iteration 1 [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Write a function "newtongraph1" which takes five arguments: an expression, a variable, an initial value, and two parameters for the plotting range. The function should draw a plot of the expression on the given plotting range (in blue), the initial value of the variable as a point on the x-axis and the corresponding point on the graph (both in red), the tangent line to that point (in green), and the x-intersection of that line (in red). This illustrates graphically how the Newton iteration improves a guess. Then run newtongraph1(x^2-1,x,1,-1,1.2). Does it look like it should? (Compare to 7.10 G.) [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Newton's iteration 2 [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Write a function "newtongraph2" which takes six arguments: an expression, a variable, an initial value, number of iterations, and two parameters for the plotting range. The function should do the same as that of the previous exercise but it should iterate the Newton approximation the given number of times and represent all steps graphically. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Solve and plot 1 [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Find the tangents to the circle x^2+y^2=7 that go through the point (-3,1) and illustrate graphically. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Solve and plot 2 [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Find all simultaneous solutions of the equations x^2+3*y^2+y^3=5 and y/3+sin(x^2)=0 and illustrate it graphically. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ /* [wxMaxima: input end ] */ /* [wxMaxima: subsubsect start ] Confusions [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] What was something you realized during the course that you wish you were clearly told on day one? Explain it here to a new student so that he or she can avoid the same pitfall. [wxMaxima: comment end ] */ /* [wxMaxima: section start ] LaTeX [wxMaxima: section end ] */ /* [wxMaxima: comment start ] LaTeX is a typesetting software used in various fields of science. These exercises assume some basic familiarity with LaTeX, but not much. Getting started with LaTeX is not within the scope of this course. We recommend two options: - Download and install MiKTeX. It comes with LaTeX in full glory and an editor to go with it. - Use Overleaf, an online service that runs LaTeX on a server. Both options are free. Overleaf requires a paid licence if you want to collaborate with more than two people on a single project. Some exercises below may require you to search online how to use certain LaTeX commands. [wxMaxima: comment end ] */ /* [wxMaxima: subsect start ] Exercises [wxMaxima: subsect end ] */ /* [wxMaxima: comment start ] In most of the exercises here you need to produce a PDF file with LaTeX. Return those files, and name them as follows: SLX-EN-Smith-512.pdf Replace "Smith" with your last name and "512" with the number of the exercise without dots. [wxMaxima: comment end ] */ /* [wxMaxima: subsubsect start ] Export wxm to LaTeX [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Open exercise package C and evaluate all cells. Then click File > Export (in Finnish: Tiedosto > Vie) and choose the saving type .tex from the drop-down menu. Compile the file with PDFLaTeX and make sure there are no errors. You may need to manually adjust some things. Add a footnote with the command \footnote{...} explaining something so that it shows within the first two pages of the final PDF file. The PDF doesn't need to be pretty here. It just needs to work. [wxMaxima: comment end ] */ /* [wxMaxima: subsubsect start ] Maxima pictures to LaTeX [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Choose an expression or function you like. (Not a constant!) Use Maxima to draw a plot that shows the function and its derivative together. Save that picture. Create a simple .tex file containing a figure environment that includes a picture and a caption and a label. Put your picture into it and write a caption telling what the picture is about. Outside the environment, add some text and a reference to your figure using \ref{...}. [wxMaxima: comment end ] */ /* [wxMaxima: subsubsect start ] Extreme value problem [wxMaxima: subsubsect end ] */ /* [wxMaxima: comment start ] Find the smallest and largest value of the function f(x)=x*log(3+x)-sin(x^2) on the interval [0,5]. Create a PDF file with LaTeX containing the following: - the function - its derivative (compute with Maxima) - zeroes of the derivative (numerically with Maxima) - values of the function at critical points and endpoints (compute with Maxima) - a plot showing the function on the right interval and the values computed above as points on the graph (draw with Maxima) - a statement of what and where the minimum and maximum are [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ wxplot2d(x*log(3+x)-sin(x^2),[x,0,5]); /* [wxMaxima: input end ] */ /* Maxima can't load/batch files which end with a comment! */ "Created with wxMaxima"$