Solve an equation that has data arrays as constants

1 view (last 30 days)
Hello,
I want to solve the following equation:
1/c0 c^2 - c +X*t / c0 = 0
where c0 is a given constant but X and t are constant arrays: they are data sets where X varies with time, t. The goal is to solve for c, which too will be a data array that varies with time, t.
I tried:
func = @(c) 1/c0.*c^2 - c + X.*t/c0;
c = fzero(func,1)
But got errors likely because X and t are arrays and not single values. Any other ideas? Time sensitive, thank you!!!

Accepted Answer

Walter Roberson
Walter Roberson on 2 Dec 2021
You have c^2 and say that c will be an array.
Could you confirm that you c^2 meaning c*c meaning algebraic matrix multiplication (inner product) of c with itself?
If you do then all of the values have to be searched for simultaneously, which makes it a multivariate problem, but fzero() can only be used for univariate situations. fsolve() can be used for multivariate cases.
Also is X*t intended to be algebraic matrix multiplication like in your problem definition, or is it X.*t like in your code?
XTc0 = X.*t./c0;
c = arrayfun(@(xtc0) fsolve(@(c) 1/c0.*c.^2 - c + xtc0), XTc0)
  4 Comments
Walter Roberson
Walter Roberson on 2 Dec 2021
XTc0 = X.*t./c0;
can be computed in advance, since it does not depend upon the current c value.
Because of that, your problem comes down to solving
1/c0.*c.^2 - c + xtc0
repeatedly where xtc0 iterates over all values in the XTc0 matrix.
But for any particular value, that is just a quadratic equation, with general form as given by "ans =" above. And that in turn means that you can compute all of the solutions without using arrayfun or fsolve: just program the first of the two formulas and replace xtc0 with X.*t./c0 :
c_plus = sqrt(c0) .* (sqrt(c0) + sqrt(c0 - 4 * X.*t./c0))/2;
should now be the entire set of solutions.
If the above is real-valued then there would also be another set of real-valued solutions
c_minus = sqrt(c0) .* (sqrt(c0) - sqrt(c0 - 4 * X.*t./c0))/2;
which might or might not be positive, depending on the values involved.

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!