2016-12-20 29 views
1

我正在研究matlab中的picard方法。這需要在一個被代入的函數上進行多次迭代以成爲積分多項式。Matlab Picard方法 - 將現有的symfun分配給矢量

現在我有一個現有的多項式SYMS x,它是由一些向量定義:

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 

但是當我嘗試使用y(1) = symfun(polynomial,x);,給我一個錯誤失敗:

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 ':'.

但由於polynomial只是一個symfunction,爲什麼不能將它分配給向量中的插槽?我已經嘗試使用feval()而不是使用symfun()同時定義,但目前爲止沒有任何工作。

完整代碼:

%%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 

提前感謝!

+0

你的代碼的其餘部分在哪裏?最初如何定義「多項式」,「度數」等?請提供一個最簡單的工作示例,以便其他人可以嘗試複製您的錯誤? 'polynomial'已經是'x'中的'symfun' - 爲什麼要調用'y(1)= symfun(polynomial,x);'而不是'y(1)= polynomial;'? – horchler

+0

我用完整的代碼更新了它。 'y(1)= symfun(polynomial,x);'是一種嘗試,因爲它沒有它也不起作用。但是,是沒有道理的。 – Wickea

回答

1

修好了! 這是工作代碼。 (可能仍然有輕微的錯誤)。這個錯誤並沒有正確定義polynomial,因爲我第一次有syms(polynomial),它導致多項式的和包含「多項式」作爲一個變量。考慮到picard方法,例如y(1)的定義,也修正了一些輕微的其他錯誤。

%%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 

%%vector print 

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 = polynomial + symfun(a(i) * x ^(i-1), x); 

end 

%Picard Method 
syms t; 
syms y; 
y = sym('y', [1 maxIterations]); 
y(1) = x0; 

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('The iteration y_%d = %s \n',(i-1), y(i)) 

end 

%plot graphs