How to sub in multiple vectors into a structure

조회 수: 5 (최근 30일)
ETHAN FONTANA
ETHAN FONTANA 2020년 6월 29일
댓글: Walter Roberson 2020년 6월 30일
Hello MATLAB community,
I am currently writing a program that contains a structure full of symbolic equations and I need eto evaluate those equations. I am attemtpting to sub in values for certian symbolic variables that are 91x1 vectors. I do not know how to successfully sub in multiple double-valued vectors into a single equation to replace symbolic variables and get a result vector. This is the code I have currently:
Sol_struct.Ax = double(subs({o2 o5 o6 x5 dx Fs}, {Vector_matrix}))
Where o2, o5, o6, x5, dx, and Fs are all 91x1 column vectors, and Sol_struct.Ax is one out of 18 fields of the Sol_struct matrix. It returns a 91x36 double, instead of the expected 91x1 double.
Thank you for any help!
  댓글 수: 1
madhan ravi
madhan ravi 2020년 6월 29일
편집: madhan ravi 2020년 6월 29일
size(vector_matrix) %?
Please show a short example with a desired output.

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

채택된 답변

Walter Roberson
Walter Roberson 2020년 6월 29일
vars = num2cell([o2; o5; o6; x5; dx; Fs]);
Sol_struct.Ax = double(subs(Sol_struct.Ax, vars, Cell_matrix));
Cell_matrix would have to be a (91*6) x 1 cell array containing the replacement values. Each entry could be non-scalar, but the sizes would have to be consistent.
If you are trying to substitute a scalar for each variable, then
vars = [o2; o5; o6; x5; dx; Fs];
Sol_struct.Ax = double(subs(Sol_struct.Ax, vars, Vector_matrix));
Vector_matrix would be a (91*6) x 1 numeric vector.
  댓글 수: 2
ETHAN FONTANA
ETHAN FONTANA 2020년 6월 30일
Hello Walter, thank you very much for your answer. I have implemented that code, but for some reason the values do not seem to be subsituting. I will provide code below.
% creating symbolic vector valued variables
o2 = sym(zeros(91,1));
o5 = sym(zeros(91,1));
o6 = sym(zeros(91,1));
x5 = sym(zeros(91,1));
dx = sym(zeros(91,1));
Fs = sym(zeros(91,1));
%Evaluating Vectored Valued Variables
var_cell = num2cell([o2; o5; o6; x5; dx; Fs]);
cell_matrix = num2cell(vector_data)
Sol_struct.Ax = double(subs(Sol_struct.Ax, var_cell, cell_matrix));
which returns the following:
I am expecting a 91x1 column vector, not an equation. I know your code is correct, I am just not implementing something correctly. Thanks!
Walter Roberson
Walter Roberson 2020년 6월 30일
That code cannot return an equation. The double() would fail if there are any unresolved symbolic variables after the subs()
o2 = sym(zeros(91,1));
o5 = sym(zeros(91,1));
o6 = sym(zeros(91,1));
x5 = sym(zeros(91,1));
dx = sym(zeros(91,1));
Fs = sym(zeros(91,1));
Those are wrong. Before you started building the equation, you should have used either
syms o2 91 1
syms o5 91 1
syms o6 91 1
syms x5 91 1
syms dx 91 1
syms Fs 91 1
or
o2 = sym('o2_', 91, 1);
o5 = sym('o5_', 91, 1);
o6 = sym('o6_', 91, 1);
x5 = sym('x5_', 91, 1);
dx = sym('dx_', 91, 1);
Fs = sym('Fs_', 91, 1);
Also note that vector_data will need to be a (91*6) x 1 numeric vector.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by