Clear Filters
Clear Filters

how to implement circular reference in MATLAB

8 views (last 30 days)
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!!

Accepted Answer

John D'Errico
John D'Errico on 6 Apr 2017
Edited: John D'Errico on 6 Apr 2017
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 Comments
Shravan Kumar
Shravan Kumar on 6 Apr 2017
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 on 6 Apr 2017
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!