Mathematica Homework Issue: Missing Points, Scaling Problem

  • Thread starter CornMuffin
  • Start date
  • Tags
    Mathematica
In summary, the conversation discusses a failure to plot all points in a code for approximating the solution of a given equation using the Runge-Kutta method of order 4. The problem is resolved by using the Sow and Reap functions and setting the x-axis values with DataRange.
  • #1
CornMuffin
55
5

Homework Statement


When I put the this code into Mathematica, it fails to plot all of the points... there are some missing points, so i decided to also tell it to display the table so I can look at the values, but some are not defined, and it puts something like y[0.95], y[0.35] or y[0.] instead. Also, I am having another problem with the scaling... it is labeling 0.05 as 1, 0.10 as 2, and so on, on the graph.

And if it helps, this Code is supposed to approximate the solution of y'=-ty+4t/y when 0<= t <= 1, where y(0)=1 and the step size h=0.05 using Runge-Kutta method of order 4. And then I need it to plot the graph using the data that I obtained.

Homework Equations





The Attempt at a Solution


Here is my code that I put into Mathematica
Code:
w = 1; h = 0.05; t = 0; y[0] := 0;
While[t < 1, k1 = h ((-t) (w) + (4 t)/w);
  k2 = h (-(t + h/2) (w + k1/2) + (4 (t + h/2))/(w + k1/2));
  k3 = h (-(t + h/2) (w + k2/2) + (4 (t + h/2))/(w + k2/2));
  k4 = h (-(t + h) (w + k3) + (4 (t + h))/(w + k3));
  w = w + (1/6) (k1 + 2 k2 + 2 k3 + k4); t = t + h; y[t] = w];
datapts = Table[y[t], {t, 0, 1, 0.05}];
Print[datapts];
ListPlot[datapts]
And I realize now that it would have been easier to define f(x,t)... but that should affect it... lol




The output for the table is:
Code:
{y[0.],1.00374,1.01482,1.03283,1.05718,1.08709,1.1217,y[0.35],1.20149,y[0.45],1.28981,1.33533,1.38093,1.42611,1.47042,y[0.75],y[0.8],y[0.85],y[0.9],y[0.95],y[1.]}


And this is what the graph looks like:
http://img695.imageshack.us/img695/941/problem5.gif [Broken]
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
I just learned about Sow and Reap. Try it out (and note the use of DataRange for setting the x-axis values):

Code:
w = 1; h = 0.05; t = 0;
datapts = Reap[While[t < 1, k1 = h ((-t) (w) + (4 t)/w);
    k2 = h (-(t + h/2) (w + k1/2) + (4 (t + h/2))/(w + k1/2));
    k3 = h (-(t + h/2) (w + k2/2) + (4 (t + h/2))/(w + k2/2));
    k4 = h (-(t + h) (w + k3) + (4 (t + h))/(w + k3));
    w = w + (1/6) (k1 + 2 k2 + 2 k3 + k4); t = t + h; Sow[y[t] = w]]];
ListPlot[Flatten[datapts[[2 ;;]]], DataRange -> {0, 1 - h}]
 
  • #3
Mapes said:
I just learned about Sow and Reap. Try it out (and note the use of DataRange for setting the x-axis values):

Code:
w = 1; h = 0.05; t = 0;
datapts = Reap[While[t < 1, k1 = h ((-t) (w) + (4 t)/w);
    k2 = h (-(t + h/2) (w + k1/2) + (4 (t + h/2))/(w + k1/2));
    k3 = h (-(t + h/2) (w + k2/2) + (4 (t + h/2))/(w + k2/2));
    k4 = h (-(t + h) (w + k3) + (4 (t + h))/(w + k3));
    w = w + (1/6) (k1 + 2 k2 + 2 k3 + k4); t = t + h; Sow[y[t] = w]]];
ListPlot[Flatten[datapts[[2 ;;]]], DataRange -> {0, 1 - h}]

Thank you very much :)
 

1. Why are there missing points in my Mathematica homework?

There can be several reasons for missing points in your Mathematica homework. One possibility is that you have made a mistake in your code, leading to incorrect output. Another reason could be that you have not followed the instructions properly or have missed a step in the problem. It is important to carefully review your code and the instructions to identify the cause of missing points.

2. How can I fix the scaling problem in my Mathematica homework?

If you are facing a scaling problem in your Mathematica homework, it could be due to incorrect scaling parameters in your code. Make sure to review the scaling options and adjust them accordingly. You can also try using the "PlotRange" option to manually set the range of values for your plot. If the issue persists, consult with your professor or a teaching assistant for further assistance.

3. Can I use Mathematica to solve complex mathematical equations?

Yes, Mathematica is a powerful software that can handle complex mathematical equations and problems. It has a wide range of built-in functions and algorithms that can help you solve various mathematical problems efficiently. You can also define your own functions and equations in Mathematica to solve specific problems.

4. How do I troubleshoot errors in my Mathematica code?

If you encounter errors in your Mathematica code, the first step is to carefully read the error message to identify the cause of the error. It could be due to a syntax error, an undefined variable, or an incorrect function call. Make sure to check your code for any typos or mistakes. You can also consult the Mathematica documentation or online resources for troubleshooting tips.

5. Can I use Mathematica to create visualizations for my data?

Yes, Mathematica has powerful visualization capabilities that allow you to create various types of plots and graphs for your data. You can use built-in functions such as "ListPlot" or "BarChart" to generate visualizations, or customize your plots using options and styling features. Mathematica also supports 3D plotting and animations, making it a versatile tool for visualizing data.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
742
  • Engineering and Comp Sci Homework Help
Replies
6
Views
788
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
792
Replies
5
Views
292
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
803
  • Programming and Computer Science
Replies
15
Views
2K
Back
Top