I am working on a program for the picard method in matlab. This requires multiple iterations over a function being substituted in a to be integrated polynomial.
Now I have an existing polynomial with syms x, which is defined by some vector a:
for i = 1:degree+1
polynomial = symfun(polynomial + a(i) *x^(i-1), x);
end
syms t;
y = symfun( zeros(1,maxIterations+1),x);
y(1) = polynomial;
for i = 2: maxIterations
substitute = subs(polynomial, x, y(i-1));
y(i) = symfun(x0 + int( substitute, t), x); %for some predefined x0
end
But it fails when I try to use y(1) = symfun(polynomial,x);
, giving me an error:
Invalid indexing or function definition. When defining a function, ensure that the arguments are symbolic variables and the body of the function is a SYM expression. When indexing, the input must be numeric, logical, or ':'.
But as polynomial
is just a symfunction, why can't it be assigned to a slot in the vector y? I already tried using feval()
and not using symfun()
whilst defining, but nothing worked so far.
Complete code:
%%Input
prompt = 'What is the degree of the polynomial? \n ';
degree = input(prompt);
a = zeros(degree+1,1);
for i = 1:degree+1
a(i) = input(['Enter value of coefficient a_' num2str(i-1) ': ']);
end
prompt = 'What is the number of iterations? \n ';
maxIterations = input(prompt);
prompt = 'What is the value of x0? \n ';
x0 = input(prompt);
%%Computing Integrated Vector
aInt = [0];
for i = 1:degree+1
aInt(i+1) = a(i)/(i);
end
fprintf('x0');
for i = 1: degree+1
fprintf( [' + ' num2str(aInt(i+1)) ' x^' num2str(i)] );
end
fprintf('\n');
syms x;
polynomial = 0;
for i = 1: degree+1
polynomial = symfun(polynomial + a(i) * x ^(i-1), x);
end
%Picard Method
syms t;
syms y;
y = symfun( zeros(maxIterations+1,1), x );
y(1) = polynomial;
for i = 2: maxIterations
substitute = subs(polynomial, x, y(i-1));
y(i) = symfun(x0 + int( substitute,t ), t);
end
for i = 1:maxIterations
fprintf(['x_' num2str(i-1) ' = ' y(i), ' \n']);
end
Thanks in advance!
Fixed it! This is the working code. (might still have slights errors in it). The mistake was not properly defining
polynomial
, as I first hadsyms(polynomial)
which caused the sum of the polynomial to include `polynomial' as a variable. Also fixed some slight other errors considering the picard method such as the definition of y(1).