Solving an equation with two vectors

1 view (last 30 days)
Laura Streib
Laura Streib on 13 Jan 2022
Commented: Torsten on 13 Jan 2022
I am trying to solve for an unknown using an equation with two vectors. I have tried using solve but the output is a 0x1 vector. I am trying to solve for p in the equation below and want a 37411x1 vector output with all the solutions. I considered using linsolve, but it looks like that only works for equations with one vector.
TotalDensity=WaterContent{:,2};%37411x1 vector
Drydensity=WaterContent{:,5};%37411x1 vector
syms p
eqn = TotalDensity==p*.99819+(1-p)*Drydensity;
S=solve(eqn,p);
  1 Comment
Torsten
Torsten on 13 Jan 2022
TotalDensity=WaterContent{:,2};%37411x1 vector
Drydensity=WaterContent{:,5};%37411x1 vector
TotalDensity = cell2mat(TotalDensity);
DryDensity = cell2mat(DryDensity);
p = (TotalDensity - DryDensity)./(.99819 - DryDensity)

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 13 Jan 2022
You have 37411 equations in one single variable. There is no value, p, such that all of the equations are satisfied simultaneously.
solve() is for simultaneous equations. For example,
syms x y
solve([x + y == 5, 3*x + 4*y == 5])
ans = struct with fields:
x: 15 y: -10
Notice that this does not attempt to solve for ((x + y == 5) OR (3*x + 4*y == 5)) -- it solves for ((x + y == 5) AND (3*x + 4*y == 5))
There are two strategies:
  1. For linear equations and low-degree polynomials, solve a template and then subs() in the actual numeric values to get the vector of results; OR
  2. for nonlinear equations, loop doing one equation at a time, perhaps using arrayfun()
syms TD DD p
eqn = TD == p*.99819+(1-p)*DD;
S = solve(eqn, p)
S = 
%some numeric values for testing
TotalDensity = rand(4,1), Drydensity = rand(4,1).^2
TotalDensity = 4×1
0.9501 0.5393 0.9953 0.9124
Drydensity = 4×1
0.4446 0.5514 0.2766 0.2357
%get numeric solutions
SN = double(subs(S, {TD, DD}, {TotalDensity, Drydensity}))
SN = 4×1
0.9131 -0.0271 0.9961 0.8875

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!