clear all
%asks MATLAB to print out more decimal places.
format long e
%
% It is important to have a good guess!!
%
xmatlab1 = fsolve(@TwoPointGaussLegendreQuadrature,[1;1;-1;1]);
options = optimoptions(@fsolve,’Display’,’iter’,’SpecifyObjectiveGradient’,true);
[xmatlab2,F,exitflag,output,JAC] = fsolve(@TwoPointGaussLegendreQuadratureWithJacobian,[1;1;-1;1],options);
function [f,J] = TwoPointGaussLegendreQuadratureWithJacobian(x)
f(1) = x(1)+x(2)-2;
f(2) = x(1)*x(3)+x(2)*x(4);
f(3)=x(1)*x(3).^2+x(2)*x(4).^2-(2./3.);
f(4)=x(1)*x(3).^3+x(2)*x(4).^3;
J(1,1)=1;J(1,2)=1;J(1,3)=0;J(1,4)=0;
J(2,1)=x(3);J(2,2)=x(4);J(2,3)=x(1);J(2,4)=x(2);
J(3,1)=x(3).^2;J(3,2)=x(4).^2;J(3,3)=2*x(1)*x(3);J(3,4)=2*x(2)*x(4);
J(4,1)=x(3).^3;J(4,2)=x(4).^3;J(4,3)=3*x(1)*x(3).^2;J(4,4)=3*x(2)*x(4).^2;
end
function f = TwoPointGaussLegendreQuadrature(x)
f(1) = x(1)+x(2)-2;
f(2) = x(1)*x(3)+x(2)*x(4);
f(3)=x(1)*x(3).^2+x(2)*x(4).^2-(2./3.);
f(4)=x(1)*x(3).^3+x(2)*x(4).^3;
end