how to create a function to solve a series of equitions?

1 回表示 (過去 30 日間)
Zhe Dong
Zhe Dong 2024 年 4 月 8 日
コメント済み: Zhe Dong 2024 年 4 月 8 日
Hi Matlab users,
I want to write a function to help me solve a series of equtions, however I'm not sure how to achieve that. here's an example. The problem is that I want to calculate the unknowns 'a','b,'c', so I need to put it in the output of the function, in the mean time they are in the equtions, so I need to define them in the function, but how to define them if they are unkonwns?
function [a,b,c] = EqSol(var1,var2,var3,var4,var5,var6)
eq1 = a + b + c == 1;
eq2 = var3 == var1*a + var2*b;
eq3 = var6*var3 == var4*var1*a + var5*var2*b + c;
eqn = [eq1,eq2,eq3];
S = solve(eqn,[a,b,c]);
end

採用された回答

VBBV
VBBV 2024 年 4 月 8 日
if you have symbolic toolbox then you can define it inside the function as
function [a,b,c] = EqSol(var1,var2,var3,var4,var5,var6)
syms a b c
  2 件のコメント
VBBV
VBBV 2024 年 4 月 8 日
編集済み: VBBV 2024 年 4 月 8 日
After solving the equations then return the outputs as
a = S.a
b = S.b
c = S.c
Assuming varx ...1,,6 are known values
Zhe Dong
Zhe Dong 2024 年 4 月 8 日
yes that works, many thanks for the help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAssembly についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by