# Untitled

## 1118 days ago by pub

We start with an easy change of variables ... Problem 2 is a change of variables for integration.

Define function to change variables

def change_var(h,oldvar,newfun): return h.substitute(oldvar==newfun)

Suppose $f(x)=2x+1$ and u(t)=$cos(t)$. Create $f(t)=f(u(t))$.

var('x t') f=2*x+1 u=cos(t) f=change_var(f,x,u) view(f)
 \newcommand{\Bold}[1]{\mathbf{#1}}2 \, \cos\left(t\right) + 1 \newcommand{\Bold}[1]{\mathbf{#1}}2 \, \cos\left(t\right) + 1

Problem 1: We are given: $f(x,y,z)=(2x-1)y$ and a vector function with 3 components: $s(t)=0.5<cos(t)+1,sin(t),cos(t)+1>$

We want $f(x,y,z)$ to become $f(t)$  by substituting the 1st component of r(t) for x, the 2nd for y and the 3rd for z ((ypical of problems with line integrals).

var ('t'); var('x y z')
 \newcommand{\Bold}[1]{\mathbf{#1}}t\newcommand{\Bold}[1]{\mathbf{#1}}\left(x, y, z\right) \newcommand{\Bold}[1]{\mathbf{#1}}t\newcommand{\Bold}[1]{\mathbf{#1}}\left(x, y, z\right)
f=(2*x-1)*y s=vector((0.5*cos(t)+0.5,0.5*sin(t),0.5*cos(t)+0.5))

Calling the function changes the variables of $f$ from a function of (x,y,z) to a function of t using s(t).

f=change_var(f,x,s[0]) view(f) f=change_var(f,y,s[1]) view(f) f=change_var(f,z,s[2]) view(f)
 \newcommand{\Bold}[1]{\mathbf{#1}}y \cos\left(t\right)\newcommand{\Bold}[1]{\mathbf{#1}}0.500000000000000 \, \sin\left(t\right) \cos\left(t\right)\newcommand{\Bold}[1]{\mathbf{#1}}0.500000000000000 \, \sin\left(t\right) \cos\left(t\right) \newcommand{\Bold}[1]{\mathbf{#1}}y \cos\left(t\right)\newcommand{\Bold}[1]{\mathbf{#1}}0.500000000000000 \, \sin\left(t\right) \cos\left(t\right)\newcommand{\Bold}[1]{\mathbf{#1}}0.500000000000000 \, \sin\left(t\right) \cos\left(t\right)

Problem 2: We want to solve the integral $\int_0^2{xcos(x^2+1)dx}$ by doing a change of variable $u=x^2+1$ (so that $x=\sqrt{u-1}$).

Define function changevar to change f(x)*dx to f(u)*du

def changevar(h, oldvar,newfun, newvar): dx = diff(newfun,newvar) return h.substitute(oldvar==newfun)*dx
var('x');
 \newcommand{\Bold}[1]{\mathbf{#1}}x \newcommand{\Bold}[1]{\mathbf{#1}}x
f=x*cos(x^2+1) xmin=0; xmax=2
var ('u'); t=sqrt(u-1)
ans=changevar(f, x, t, u) view(ans)
 \newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{2} \, \cos\left(u\right) \newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{2} \, \cos\left(u\right)
#I do not know how this works. It looks like a mini-programming function that solves the equation t==bound whose solution is a list of the form (u=answer). #Since we only want "answer", we extract the element with [0] and take the rhs=righthandside. var('bound') assume(bound>0) limits(bound)=solve(t==bound,u)[0].rhs()
int=integrate(ans,(u,limits(xmin),limits(xmax))) view(int)
 \newcommand{\Bold}[1]{\mathbf{#1}}-\frac{1}{2} \, \sin\left(1\right) + \frac{1}{2} \, \sin\left(5\right) \newcommand{\Bold}[1]{\mathbf{#1}}-\frac{1}{2} \, \sin\left(1\right) + \frac{1}{2} \, \sin\left(5\right)
#Use float() or n() to get decimal answer float(int)
 \newcommand{\Bold}[1]{\mathbf{#1}}-0.900197629736 \newcommand{\Bold}[1]{\mathbf{#1}}-0.900197629736
#or apparently a .n() at the end gives a decimal answer integrate(ans,(u,limits(xmin),limits(xmax))).n()
 \newcommand{\Bold}[1]{\mathbf{#1}}-0.900197629735517 \newcommand{\Bold}[1]{\mathbf{#1}}-0.900197629735517