
#priklady na hledani korenu/ reseni soustav nelinearnich rovnic
function cviceni7

#funkce jedne promenne

osax=linspace(-5,5,100);
plot(osax,fce1(osax));
grid("on");


x=fzero(@fce1,0.5)
fce1(x)

[x,fval,info,output]=fzero(@fce1,0.5)

[x,fval,info,output]=fzero(@fce1,0.5,optimset("TolX",0.1))
[x,fval,info,output]=fzero(@fce1,[0.5,0.8],optimset("TolX",1e-11))

osax=linspace(-15,15,100);
plot(osax,fce2(osax))
grid("on")

x=fzero(@fce2,2)
x=fzero(@fce2,6)
x=fzero(@fce2,[5,9])



#polynomy

coef=[1,-3,2];
roots(coef)

coef=[1,0,1];
roots(coef)

coef=rand(1,10);
roots(coef)


# systemy rovnic


[x,fval,info,output]=fsolve(@fce3,[1,1],optimset("TolX",1e-12,"TolFun",1e-12))

[x,fval,info,output]=fsolve(@fce4,[1,1],optimset("TolX",1e-12,"TolFun",1e-12,"Jacobian","on"))



[x,fval,info]=fsolve(@fce5,0.5+1.4I,optimset("ComplexEqn","on"))



endfunction



function y=fce1(x)
y=cos(x)-x;
endfunction

function y=fce2(x)
y=besselj(1,x)./x;
endfunction

function y=fce3(x)
y(1)=x(1)^2+x(1)*x(2)+sin(x(2))-1;
y(2)=x(1)^2-x(1)*x(2)^2+cos(x(1))+1;
endfunction

function [y,jac]=fce4(x)
y(1)=x(1)^2+x(1)*x(2)+sin(x(2))-1;
y(2)=x(1)^2-x(1)*x(2)^2+cos(x(1))+1;
jac(1,1)=2*x(1)+x(2);
jac(1,2)=x(1)+cos(x(2));
jac(2,1)=2*x(1)-x(2)^2-sin(x(1));
jac(2,2)=-2*x(1)*x(2);
endfunction


function y=fce5(x)
y=x.^2-I;
endfunction