## Primeri uporabe newton.m in krivulja.m / Examples of use of newton.m and krivulja.m

To use the function newton.m, we first need to define the function $$\mathbf{F}$$ and its Jacobi matrix $$J\mathbf{F}$$. Inline functions in Octave can be defined as:

octave:1> F = @(X) [X(1)^2 - X(2)^2 - 1; X(1) + X(2) - X(1)*X(2) - 1]octave:2> JF = @(X) [2*X(1), -2*X(2); 1 - X(2), 1 - X(1)]

The above defines the function $$\mathbf{F}$$ (and corresponding Jacobi matrix $$J\mathbf{F}$$) for the example nonlinear system in the 1st exercise. We can now run Newton's iteration:

octave:3> newton(F, JF, [2; 1])

and check that we have actually obtained a solution:

octave:4> F(ans)

For the 2nd exercise (and function krivulja.m) we need to define $$f$$ and $$\mathrm{grad}\, f$$ (as inline functions). Let's do this for an ellipse $$x^2 + \frac{y^2}{4} = 1$$ first:

octave:5> f = @(x, y) x^2 + y^2/4 - 1octave:6> gradf = @(x, y) [2*x; y/2]

Then call:

octave:7> K = krivulja(f, gradf, [1; 0], 0.1, 20);octave:8> plot(K(1, :), K(2, :))
This only draws a portion of our ellipse. A call:

octave:9> K = krivulja(f, gradf, [1; 0], 0.1, 100);
will give us enough points to draw a full ellipse. In fact, we have fewer than 100 consecutive points 0.1 units apart:

octave:10> length(K)

Other implicitly given curves, e.g. $$x^4 + y^2 - x^2 y = 1$$, are also (usually) not a problem (with smaller $$\delta$$):

octave:11> f = @(x, y) x^4 + y^2 - x^2*y - 1octave:12> gradf = @(x, y) [4*x^3 - 2*x*y; 2*y-x^2]octave:13> K = krivulja(f, gradf, [1; 0], 0.05, 200);octave:14> plot(K(1, :), K(2, :))

Last modified: Sunday, 22 March 2020, 4:46 PM