how to implement circular reference in MATLAB

8 次查看(过去 30 天)
I am trying to implement an Excel formula which has circular reference. I used a iterative method to implement this in MATLAB but it takes more iterations than excel. Any suggestions or better ideas to implement circular referencing in MATLAB. Thanks in advance!!

采纳的回答

John D'Errico
John D'Errico 2017-4-6
编辑:John D'Errico 2017-4-6
But this is not excel. circular references don't apply. You are no longer using a spreadsheet. Why does this matter? Really, you can use any tool you like, in any way that you want. But as long as you stay thinking about things in terms of a spreadsheet, you will solve your problems as if you were still using a spreadsheet, just making MATLAB work as if it was a spreadsheet. You will be able to solve far more sophisticated problems once you begin using MATLAB as it was designed to be used.
That it takes more iterations merely means you did probably a poor job of writing the iterative method. But since we don't see what you did, how can we solve that? Anyway, the number of iterations required by any solver will be problem and algorithm dependent, as well as dependent on the tolerances set by the user/programmer.
In general, it is never a good idea to write your own iterative solver anyway. Instead, use the many existing tools.
To solve for a root of one equation in one unknown, you have fzero. It will be efficient, but is designed to solve single variable problems.
With more unknowns, use a solver like fsolve, from the optimization toolbox.
If you have the symbolic toolbox, you can then use tools like solve or vpasolve.
  2 个评论
Shravan Kumar
Shravan Kumar 2017-4-6
Hi John, First of all thanks for the reply!! let me try to explain what I wanted to do, may be then you can judge.
the circular reference what I am talking about is, say I want to calculate B, to calculate B I need to input some parameter called A. But A is not a direct input rather it refers to itself (A) and depends on B too. Now its not a quadratic equation or a unknown solution. Just like any other solver I indeed start at 0, but later I dont try random values to fit the function rather I have some conditions based on which I estimate the value of A.
sorry for the long and poor explanation. wish I could have pasted the code but that wouldnt make any sense without the actual logic explained.
thanks again!!
John D'Errico
John D'Errico 2017-4-6
So you have some relations between two variables named A and B.
I'll be lazy and make up some simple ones, without even thinking if there is a solution.
A = B^2 + 2
B = sin(A)
In this case, the simplest solution might be to throw it at solve, although no analytical solution can exist, so I will just use vpasolve.
syms A B
Eq1 = A == B^2 + 2;
Eq2 = B == sin(A);
result = vpasolve(Eq1,Eq2,A,B)
result =
struct with fields:
A: [1×1 sym]
B: [1×1 sym]
result.A
ans =
2.4282216728901276944572660525076
result.B
ans =
0.65438648587064181093169191106166
Since there may be multiple solutions, if your preference is another solution, then since vpasolve is a numerical solver, just use appropriate starting values.
Or, we can just recognize that we can eliminate one of the variables.
fun = @(B) B - sin(B.^2 + 2);
B = fzero(fun,1)
A = B.^2 + 2
B =
0.654386485870642
A =
2.42822167289013
Or I could have used fzolve, much as I used vpasolve.
BY the way, it is often dangerous to use zero as a starting point for an optimization, but that can be solver dependent. I'd avoid it in general.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by