ReplaceAll - Challenge Question

Combing ReplaceAll and Table can provide a powerful way of carrying out a lot of work with minimum coding. Combing these commands will be particularly useful when you are using numerical techniques, and get a very long list of replacement rules. Below is an example which allows you to practice the core technique of:
 
1. Solving a simple problem
2. Generalising the problem and solving
3. Translating the replacement rules into a list of values
4. Plotting the values

Question: Consider the differential equation y’(x) = n*sin(n*x) with the initial condition y(0) = -1.

Step 1

a) Write the above differential equation into Mathematica for the case n=1, and check your code is working.

DSolve[{y'[x]==Sin[x],y[0]==-1},y[x],x]

b) Make a function depending on n for the general case.

f[n_]:= DSolve[{y'[x]==n*Sin[n*x],y[0]==-1},y[x],x]

f[1]

Step 2

Use table to make a list of replacement rules for n from 0 to 5.

sol=Table[f[n],{n,1,5}]

Step 3

Change the replacement rules into functions.

sol2= Table[ReplaceAll[y[x],sol[[n]]],{n,1,5}]

Step 4

a) Make a plot of your solutions over the range 0 to π.

Plot[sol2,{x,0,π}]

b) Make a plot that only includes the case for n=3.

Plot[sol2[[3]],{x,0,π}]