As my first Mathematica assignment, I need to solve and then plot a linear IVP. I understand that I must use DSolve. However, when attempting to plot my output, nothing shows.

My equation is

$\qquad x\,y'+ 2 \cos(x^2)\,y = 0 \quad y(1) = 2$

I understand that this is not a very difficult problem, but I am struggling to understand the Mathematica way of expressing the problem.


closed as off-topic by Johu, eyorble, xzczd, bbgodfrey, Bob Hanlon Oct 3 '18 at 20:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Johu, eyorble, xzczd, bbgodfrey, Bob Hanlon
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    $\begingroup$ It is hard to help without exact code. General tips: check individual elements, check the built in documentation. If you click on any function once and press F1 you will see the documentation. Mathematica is case sensitive and Cos and cos are not the same thing. Start with very simple things, like Solve and only then try more complicated code. $\endgroup$ – Johu Sep 24 '18 at 19:33

There is a certain amount of syntax to be learnt. Note that we must make the dependence of y upon x explicit

equation = x y'[x] + 2 Cos[x^2] y[x] == 0;

initialvalue = y[1] == 2;

It is convenient to return the solution as a pure function

solution = DSolve[{equation, initialvalue}, y, x]
(* {{y -> Function[{x}, 2 E^(CosIntegral[1] - CosIntegral[x^2])]}} *)

This makes it easy to verify that it is indeed a solution and satisfies the initial condition.

equation /. solution
(* {True} *)

initialvalue /. solution
(* {True} *)

I suggest that you work through this example, looking for help (F1) on all the functions and syntax elements that you don't know already.

To see the result, you can use

Plot[y[x] /. solution, {x, 0, 10}, PlotRange -> {0, 20}]
  • $\begingroup$ When I try to plot the solution of this, nothing appears on my graph. I'm using Plot[solution[x_], {x, -10, 10}, PlotRange -> {0, 20}] at the moment. What is my issue? $\endgroup$ – Holm Roeser Sep 24 '18 at 20:06
  • 1
    $\begingroup$ solution is a rule enclosed in a list that is also enclosed in a list. Mikado's answer shows that it is {{y -> Function[{x}, 2 E^(CosIntegral[1] - CosIntegral[x^2])]}}. You can plot it the way Mikado demonstrated but the closes to the way you wrote it is to replace solution[x_] with solution[[1,1,2]][x]. That is: Plot[solution[[1, 1, 2]][x], {x, -10, 10}, PlotRange -> {0, 20}] $\endgroup$ – Jack LaVigne Sep 24 '18 at 21:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.