首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matlab Picard方法-将现有的符号赋给向量

Matlab Picard方法-将现有的符号赋给向量
EN

Stack Overflow用户
提问于 2016-12-20 15:59:16
回答 1查看 2.6K关注 0票数 2

我正在为matlab中的picard方法编写一个程序。这需要对要被积分多项式替换的函数进行多次迭代。

现在我有一个已有的带有syms x的多项式,它由一个向量a定义:

代码语言:javascript
复制
 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);时,它失败了,给出了一个错误:

无效的索引或函数定义。在定义函数时,请确保参数是符号变量,函数的主体是SYM表达式。索引时,输入必须是数字、逻辑或“:”。

但是,既然polynomial只是一个辛函数,为什么不能将它分配给向量y中的一个槽呢?在定义时,我已经尝试过使用feval(),而不是使用symfun(),但是到目前为止没有什么效果。

完整代码:

代码语言:javascript
复制
    %%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

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2016-12-21 11:48:41

修好了!这是工作密码。(其中可能仍有轻蔑的错误)。这个错误没有正确地定义polynomial,因为我首先使用了syms(polynomial),它导致多项式的和将‘多项式’作为一个变量。考虑picard方法,还修正了一些轻微的错误,如y(1)的定义。

代码语言:javascript
复制
%%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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41246418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档