How do I convert symbolic values from a matrix to numeric?

21 ビュー (過去 30 日間)
Alan
Alan 約18時間 前
I used a matrix to create symbolic values.
syms v [n,1]
% Creates a matrix of height n with variables v1, v2...vn
%v =
%
% v1
% v2
% vn
%
% etc
I later use those variables to create other equations and then want to solve. The problem is that I am creating n variables and I can't set v1=1, v2=2, etc since n will be different each time it runs and I can't just refer to v(1), v(2), or v(n) since that changes the value of the matrix element, not the symbolic variable v1, v2, or vn. How do I set v1, v2, and everything up to vn equal to a numeral value?
  2 件のコメント
Image Analyst
Image Analyst 38分 前
Why do you need symbolic variables at first and then need to digitize them? Why can't you just start with normal non-symbolic variables right from the start?
Alan
Alan 約5時間 前

I need to take the derivative of the equations and then solve. I'm doing power flow analysis using Newton-Raphson which creates very large matrices with a lot of variables. I thought the easiest way to do that would be to create the equations with symbolic variables then set all the variables to their numerical values instead of trying to use solve() individually on each equation.

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

回答 (1 件)

Walter Roberson
Walter Roberson 約6時間 前
n = 5;
syms v [n,1]
v
v = 
v(1)
ans = 
So indexing v does work to return the corresponding symbolic variable.
Meanwhile, to substitute for each v value,
f = v(1)^2 + v(2) - v(3)
f = 
subs(f, v, [3 -1 2 4 2].')
ans = 
6
  2 件のコメント
Alan
Alan 約5時間 前

What I was hoping to do is substitute all the values in v with their numerical values at once rather than solve one equation at a time. I'm creating a jacobian matrix of power buses which will use multiple matrices of variables like the v matrix above. One of the equations might look like:

p1 = v1*v23*y(1,23)*cos(delta1-delta3-theta(1,23))

There will be hundreds of equations like this so you can probably see it would be easier to just substitute the symbolic variables with numeric values.

Walter Roberson
Walter Roberson 約3時間 前
subs(EXPRESSIONS, v, VECTOR_OF_VALUES)
will process EXPRESSIONS substituting in all of the values in v with their corresponding numerical values and returning the modified version of EXPRESSIONS.
There is no way to say that you want the variables to be substituted with their corresponding values for all symbolic expressions .
For example,
syms x y
f1 = x^2 + y
f1 = 
f2 = x^3 - y^2 + x*y
f2 = 
X = 2; Y = 5;
%this is possible
new_f1_f2 = subs([f1, f2], {x y}, {X Y})
new_f1_f2 = 
%this does not work as you might hope
x = X; y = Y;
f1
f1 = 
f2
f2 = 
%this also does not work as you might hope
subs({x; y}, {X Y})
ans = 
f1
f1 = 
f2
f2 = 
There is no way to say "subsitute these values for x and y" and have it affect expressions you did not mention in the command -- no way to affect (say) f1 unless you include f1 in the list of expressions to be processed.
This is fundamental to the way that the Symbolic Engine works, so it is not going to be changed.

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

カテゴリ

Help Center および File ExchangeSymbolic Variables, Expressions, Functions, and Preferences についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by