Filter löschen
Filter löschen

Function to solve an equation for a given variable

3 Ansichten (letzte 30 Tage)
Simone Pagliuca
Simone Pagliuca am 22 Nov. 2022
Bearbeitet: John D'Errico am 22 Nov. 2022
I would like to have a function like f(a,b,c,d,e) which I could call multiple times in my code but to be solved for different variables:
ans1 = f(X,b,c,d,e) it solves for the first variable, I set the other 4
ans2=f(a,b, X, d, e) solves for the third, I set the other 4
and so on.

Akzeptierte Antwort

John D'Errico
John D'Errico am 22 Nov. 2022
Bearbeitet: John D'Errico am 22 Nov. 2022
If it is symbolic problem, then it is trivial. Just assign those variables, subs them in, and use solve. WTP?
If it is a numerical problem, then just create a function handle with the one variable as an unknown.
In either case, it is trivial. So WTP?
Could you get fancier, and write a wrapper function, where you pass it in the variables you want to assign, and tell it which variable is to be solved for? Yes. Go for it. Write the code, then post it on the File Exchange. If you manage to do so with a wonderfully clean interface, others will find it useful. If you can't write that clean interface, then don't waste your time, as the above solutions are so easy to implement. And since I and others have never seen a reason to bother to come up with a nice function to do something like this that I know of, it suggests that many other authors agree with me. But go for it.

Weitere Antworten (0)

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by