0
$\begingroup$

I am getting an error that just gets stuck formatting notepad, i don't think there's anything in my code that is wrong, but I'm never sure.

f[x_, y_] = xy^2 + x^3 y;
fx[x_, y_] = 2 xy^2 + y^3;
fy[x_, y_] = 2 x^2 y + 3 xy^2;

g[x_, y_] = x^2 y - xy^4;
gx[x_, y_] = 2 xy - y^4;
gy[x_, y_] = x^2 - 4 xy^3;

u = 1.6;
v = 0.7;
For[i = 0, i < 7, i++,
  d = fx[u, v] gy[u, v] - fy[u, v] gx[u, v];
  h = (g[u, v] fy[u, v] gy[u, v])/d;
  k = (f[u, v] gx[u, v] - g[u, v] fx[u, v])/d;
  u = u + h;
  v = v + k;
  Print[u, " ", v, " ", f[u, v], " ", g[u, v]]];
$\endgroup$
2
  • 1
    $\begingroup$ Check you don't have xy (a new variable) when you mean x y (x * y). The syntax highlighting (colour of the variable names) should give you a clue. $\endgroup$
    – mikado
    Commented Sep 18, 2016 at 22:36
  • 4
    $\begingroup$ Instead of For and Print, use Table[], or even in this case NestList. $\endgroup$
    – Michael E2
    Commented Sep 18, 2016 at 23:31

1 Answer 1

1
$\begingroup$

The following errors:

Wrong: f[x_, y_] = xy^2 + x^3 y; Correctly: f[x_, y_] := x*y^2 + (x^3)*y;

I recommend to read: What is the difference between Set and SetDelayed?

Working code:

 Clear[f, fx, fy, g, gx, gy,d,h,k,u,v];
f[x_, y_] := x*y^2 + (x^3) y;

fx[x_, y_] := 2 x*y^2 + y^3;
fy[x_, y_] := 2 (x^2) y + 3 x*y^2;

g[x_, y_] := (x^2) y - x*y^4;
gx[x_, y_] := 2 x*y - y^4;
gy[x_, y_] := x^2 - 4 x*y^3;

u = 1.6;
v = 0.7;

For[i = 0, i < 7, i++,
  d = fx[u, v] gy[u, v] - fy[u, v] gx[u, v];
  h = (g[u, v] fy[u, v] gy[u, v])/d;
  k = (f[u, v] gx[u, v] - g[u, v] fx[u, v])/d;
  u = u + h;
  v = v + k;
  Print[u, " ", v, " ", f[u, v], " ", g[u, v]]
  ];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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