## Is this a correct way to use fsolve?

Since fsolve keeps giving me answers with a very small but non-zero imaginary part, which i really don’t want, I though about giving the derivative of my function. Is this a correct way to do it? Also, is there a way to tell the function not to go outside the reale line?

%g is a function that is defined by an equation. I know that is invertible

%and takes values between 0 and kappa.

function g = g(y, z, p, kappa, beta, mu, muz, sigma, sigmaz)

K = (mu^2/sigma^2)*0.5;

M = (sigmaz*mu)/sigma;

q = 1/(p-1);

alpha = (sqrt((beta-M-K)^2+4*K*(beta-muz))-beta+M+K)/(2*K);

C = beta-K*p/(1-p);

%function whose zero i need to find (with respect to w)

F = @(w) ((1-p)/C)*(kappa^q – w^q)+y+z-z*(w/kappa)^(alpha-1);

%its derivative with respect to w

J = @(w) ((1-p)/C)*(-q*(w^(q-1))) -z*((alpha-1)*w^(alpha-2))/(kappa^(alpha-1));

options = optimoptions(‘fsolve’, ‘SpecifyObjectiveGradient’, true);

fun = {F, J};

w0 = kappa/2;

g = fsolve(fun, w0, options);

endSince fsolve keeps giving me answers with a very small but non-zero imaginary part, which i really don’t want, I though about giving the derivative of my function. Is this a correct way to do it? Also, is there a way to tell the function not to go outside the reale line?

%g is a function that is defined by an equation. I know that is invertible

%and takes values between 0 and kappa.

function g = g(y, z, p, kappa, beta, mu, muz, sigma, sigmaz)

K = (mu^2/sigma^2)*0.5;

M = (sigmaz*mu)/sigma;

q = 1/(p-1);

alpha = (sqrt((beta-M-K)^2+4*K*(beta-muz))-beta+M+K)/(2*K);

C = beta-K*p/(1-p);

%function whose zero i need to find (with respect to w)

F = @(w) ((1-p)/C)*(kappa^q – w^q)+y+z-z*(w/kappa)^(alpha-1);

%its derivative with respect to w

J = @(w) ((1-p)/C)*(-q*(w^(q-1))) -z*((alpha-1)*w^(alpha-2))/(kappa^(alpha-1));

options = optimoptions(‘fsolve’, ‘SpecifyObjectiveGradient’, true);

fun = {F, J};

w0 = kappa/2;

g = fsolve(fun, w0, options);

end Since fsolve keeps giving me answers with a very small but non-zero imaginary part, which i really don’t want, I though about giving the derivative of my function. Is this a correct way to do it? Also, is there a way to tell the function not to go outside the reale line?

%g is a function that is defined by an equation. I know that is invertible

%and takes values between 0 and kappa.

function g = g(y, z, p, kappa, beta, mu, muz, sigma, sigmaz)

K = (mu^2/sigma^2)*0.5;

M = (sigmaz*mu)/sigma;

q = 1/(p-1);

alpha = (sqrt((beta-M-K)^2+4*K*(beta-muz))-beta+M+K)/(2*K);

C = beta-K*p/(1-p);

%function whose zero i need to find (with respect to w)

F = @(w) ((1-p)/C)*(kappa^q – w^q)+y+z-z*(w/kappa)^(alpha-1);

%its derivative with respect to w

J = @(w) ((1-p)/C)*(-q*(w^(q-1))) -z*((alpha-1)*w^(alpha-2))/(kappa^(alpha-1));

options = optimoptions(‘fsolve’, ‘SpecifyObjectiveGradient’, true);

fun = {F, J};

w0 = kappa/2;

g = fsolve(fun, w0, options);

end fsolve, complex values, jacobian MATLAB Answers — New Questions