Matlab fsolve 传递参数
发布日期:2021-05-07 00:04:26 浏览次数:23 分类:精选文章

本文共 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])
上一篇:如何用51单片机控制步进电机运动
下一篇:excel下载

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年03月21日 04时21分35秒