Function used in fsolve with array coefficients

Using fsolve I want to solve an equation for x which has the follwing form:

a1*f1(x)+a2*f2(x)+...+ai*fi(x)=0

where ai are known coefficients (typically read from an inout excel file), i is the index going from 1 to n and fi are some nonlinear functions of x. So the routine to solve for x should look something like this

ai=xlsread('MyFile.xls',1)
Xi=fsolve(@MyFun,X0)  
function MyFun=MyFun(x)
MyFun=...
end

How can I define MyFun knowing that everytime I read a new excel file the number of ai will change?

댓글 수: 4

Do you have access to the Symbolic Toolbox?
If the number of ai will change each time, then how are the fi being generated to match the same size?
Saeid
Saeid 2018년 10월 21일
Hi Walter, I have access to the Symbolic Toolbox. As to the fi, the problem is that every time I load the ai from a new excel file there will be a new number of coefficients.
You said that the fi are functions. You are reading the ai values in, but not the functions. You have one for each ai value, and you have an indefinite number of ai values. So you must either have a maximum number of ai values and a list of corresponding functions (of which you use the first so-many) or else you must have a way of computing the functions given the index to the function.

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt J
Matt J 2018년 10월 22일
편집: Matt J 2018년 10월 22일
You should be passing ai to MyFun. The size of the ai array can be determined inside MyFun, and from that the number of coefficients.
Xi=fsolve(@(x)MyFun(x,ai),X0) ;
function out=MyFun(x,ai)
[m,n]=size(ai);
out=...
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2018년 10월 21일

댓글:

2018년 10월 22일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by