Options for output of solve function with sequential variables
5 views (last 30 days)
Show older comments
Taylor Powell
on 13 Jul 2020
Commented: Taylor Powell
on 14 Jul 2020
Ladies and Gents,
I'm working on a code which checks the order of a polynomial and then creates that number (n=order + 1) of unknown variables. I then create a system of n linear equations and solve them using the solve function.
My current issue is being able to output the results to a 'n' variable array and THEN access the variables in a for loop for substitution. I have a 1xn symbolic array, but when I attempt to equate solve to it, it ignores the array and creates a struct.
syms t
S = sym('S', [1 (order+1)]);
A = sym('A', [1 (order+1)]);
lhs=(myequationoft1); %These are symbolic equations my code generates
rhs=(myequationoft2); %Each is a function of symbolics t and S1,S2,...SN
eqns=sym(zeros(order+1,1)); %Creates symbolic array which I can assign the equations to...
for i=1:(order+1)
eqns(i)=(subs(diff(lhs,(i-1)),t,0)==subs(diff(rhs,(i-1)),t,0));
%Assigns the (i-1)th derivative of each equation evaluated at t=0 to eqns(i)
end
A=solve(eqns,S); %This is my issue.... Straight assignment creates a struct
%I want to be able to access the items (A.S1,A.S2,...A.SN) in a for loop
0 Comments
Accepted Answer
Walter Roberson
on 14 Jul 2020
fn = fieldnames(A) ;
for n=1:length(fn)
An = A.(fn{n});
do something
end
More Answers (0)
See Also
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!