
本文共 1256 字,大约阅读时间需要 4 分钟。
Suppose I have 10 equations and 10 unknowns. In the equations there are 10 constants that I don’t optimize, say C1,C2,…C10.
Is there a way to use Fsolve and Loops to solve my equations if I have 5 different sets of constants ( 5 times C1,C2,…C10) that satisfy my original 10 equations. Simply put, instead of rewriting my 10 equations and 10 unknowns… 5 times by hand, is there a way to loop inside the @myfun m-file I wrote to solve for my 10 unknowns?
Use anonymous functions, available in MATLAB since R14 I believe.
From fsolve help:
If FUN is parameterized, you can use anonymous functions to capture the problem-dependent parameters. Suppose you want to solve the system of nonlinear equations given in the function myfun, which is parameterized by its second argument c. Here myfun is an M-file function such as
function F = myfun(x,c) F = [ 2*x(1) - x(2) - exp(c*x(1)) -x(1) + 2*x(2) - exp(c*x(2))];
To solve the system of equations for a specific value of c, first assign the value to c. Then create a one-argument anonymous function that captures that value of c and calls myfun with two arguments. Finally, pass this anonymous function to FSOLVE:
c = -1; % define parameter first x = fsolve(@(x) myfun(x,c),[-5;-5])
发表评论
最新留言
关于作者
