Creating variable functions using MATLAB

So I have three seperate function in MATLAB where each have its designated purpose.

1. The first one calculates the partial derivative

2. The second finds the roots for a system of two equations and two variables.

3. The third is supposed to find the critical point.

I have tried doing everything separately and it worked. However when I do this through my CriticalPoint function it just keeps running(it says "busy" in the lower left corner).

I have tried solving this manually. Meaning that I've used each function as intended, saved the values in my workspace and used for the next function.

The only thing different is how I treated my partials.

I have a function which takes evaluates the partial derivative at a point [x,y].

 derivative = Pderiv(f, a, b, i)

%The i denotes if the partial derivative is take with respect to x or y
%where if i == 1, then it means that partial derivative is with respect to
%x and if i == 2 then  it is with respect to y


The thing I did differently was that I found the partial derivative for f(x,y) manually using pen and paper.

I.e

$$\partial f/ \partial x$$ and $$\partial f/ \partial y$$

And then I inserted it into my function:

[x0,y0] = MyNewton(f, g, a, b)


Where $$\partial f/ \partial x$$ is the argument for $$f$$ and $$\partial f/ \partial y$$ is the argument for $$g$$.

This gives me the correct $$x$$ & $$y$$ values when $$\partial f/ \partial x=\partial f/ \partial y=0$$.

What I want to have it do is for a given function $$f(x,y)$$

I want to find the partial derivative where the input values are still represented by x and y.

I was told by my teacher that the following expression:

g=@(x,y)NPderiv(f,x,y,1);


would be sufficent. Is it though? Since it works when I do it manually but the program runs forever when I define my function g as above.

I'm not sure if it is relevant but here is my code for the partial derivative:

function derivative = NPderiv(f, a, b, i)

h = 0.0000001;
fn=zeros(1,2);

if i == 1
fn(i) = (f(a+h,b)-f(a,b))/h;

elseif i==2

fn(i) = (f(a,b+h)-f(a,b))/h;

end

derivative = fn(i);

end


function for my critical point function:

function [x,y] = CriticalPoint(f, a, b)

g=@(x,y)NPderiv(f,x,y,1); %partial derivative for f(x,y) with respect to x
z=@(x,y)NPderiv(f,x,y,2);%partial derivative for f(x,y) with respect to y

[x,y]=MyNewton(g,z,a,b);

end


Below shows a working version of what's inteded. What I would like if I could separate $$f(x,y)=(x-1)^2 + (y-1)^2$$ into two function g & z

function [x,y] = MyMinMax2(a, b)

%g=@(x,y)MyNPderiv(f,x,y,1);
%z=@(x,y)MyNPderiv(f,x,y,2);

g=@(x,y)2*x-2;
z=@(x,y)2*y-2;

[x,y]=MyNewton2(g,z,a,b);

end