Solving an equation with two vectors

조회 수: 22 (최근 30일)
Laura Streib
Laura Streib 2022년 1월 13일
댓글: Torsten 2022년 1월 13일
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
Torsten
Torsten 2022년 1월 13일
TotalDensity=WaterContent{:,2};%37411x1 vector
Drydensity=WaterContent{:,5};%37411x1 vector
TotalDensity = cell2mat(TotalDensity);
DryDensity = cell2mat(DryDensity);
p = (TotalDensity - DryDensity)./(.99819 - DryDensity)

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 1월 13일
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

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by