Msh
Msh

Reputation: 192

Matlab - Put elements of structure into an array

syms x t 
n=3; a=-1; b=1;
f=@(x) x;
k=@(x,t) x*t;
A=sym('a',[1 n]);
y(x)=A(1);
for i=1:n-1
    y(x)=y(x)+A(1,i+1)*(x^i);
end
I(A)=int((y(x)-f-int(k*y(t),t,a,b))^2,x,a,b)
for i=1:n
    S(i)=diff(I,A(1,i));
end
p=solve(S,A);

I want to use all element "p" as polynomial coefficients. How to put all elements into an array?

Upvotes: 1

Views: 78

Answers (1)

Jean-Paul
Jean-Paul

Reputation: 21150

There are two ways:

Option 1: Specify the amount of output parameters

[p1,p2,p3] = solve(S, A);
p1 = double(p1);
p2 = double(p2);
p3 = double(p3);

Option 2: Convert the output to an array using structfun

p = solve(S,A);
p = structfun(@double, p);

Result:

p =
     0
     3
     0

Upvotes: 1

Related Questions