Generate Code for
This example shows how to generate C code for solving systems of nonlinear equations
Equation to Solve
The system of nonlinear equations to solve is
Convert the equations to the form F(x) = 0.
Code Generation Steps
Write a function that computes the left side of the two equations. For code generation, your program must allocate all arrays when they are created, and must not change their sizes after creation.
function F = root2d(x) F = zeros(2,1); % Allocate return array F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2); F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5; end
Write a function that sets up the problem and calls
fsolve. The function must refer to
root2das a function handle, not as a name.
function [x,fval] = solveroot options = optimoptions('fsolve','Algorithm','levenberg-marquardt','Display','off'); fun = @root2d; rng default x0 = rand(2,1); [x,fval] = fsolve(fun,x0,options); end
Create a configuration for code generation. In this case, use
cfg = coder.config('mex');
Generate code for the
codegen -config cfg solveroot
Test the generated code by running the generated file, which is named
[x,fval] = solveroot_mex
x = 0.3532 0.6061 fval = 1.0e-14 * -0.1998 -0.1887
codegen (MATLAB Coder) |