Can someone help me with Matlab fsolve

Hi, i'm trying to run this function below, but it keeps showing error on line 242. I have no idea what i'm doing wrong. Can someone help me out!
Thanks a lot!
function F = myfun(x)
F(1) = (x(2)/x(1)) - ((0.3)/((1/0.99)-(1-0.025)))^(1/(1-0.3));
F(2) = ((1-x(1))^(2/0.025)/x(1)) - (((x(2)/x(1))^0.3 - 0.025*(x(2)/x(1))) / (1-0.3)^(1/0.025)*((x(2)/x(1)))^(0.3/0.025));
fun = @myfun;
x0 = [1,1];
x = fsolve(fun,x0)

 Accepted Answer

All the spaces are likely the problem. MATLAB interprets spaces in matrices as delimiters.
This runs (I converted your ‘myfun’ function to an anonymous function since it is more convenient for me), however it has problems converging, since it routinely stops not because it found a solution but because it has encountered its iteration limit, even when I give it a large number of function evaluations and iterations:
myfun = @(x) [(x(2)/x(1)) - ((0.3)/((1/0.99)-(1-0.025)))^(1/(1-0.3)); ((1-x(1))^(2/0.025)/x(1)) - (((x(2)/x(1))^0.3 - 0.025*(x(2)/x(1))) / (1-0.3)^(1/0.025)*((x(2)/x(1)))^(0.3/0.025))];
fun = myfun;
opts = optimoptions('fsolve', 'MaxFunctionEvaluations',1E+6, 'MaxIterations',1E+5);
x0 = [1,1];
x = fsolve(fun,x0,opts)
The result is:
Solver stopped prematurely.
fsolve stopped because it exceeded the iteration limit,
options.MaxIterations = 1.000000e+05.
x =
3.263185797543431e-05 2.372801409008184e-05
I defer to you to solve that.
If you use:
x0 = [1,1]+1i;
it finds complex roots.
You need to explore it and find out the reason it is not converging on definable roots. There could be several reasons.

2 Comments

Joao Su Man Chung’s Answer moved here —
Thanks a lot! That helped me out!
My pleasure!
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!