如何改变价值观,做功能牛顿迭代 [英] How to change the values and do the functions Newton-Raphson

查看:149
本文介绍了如何改变价值观,做功能牛顿迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要遍历牛顿迭代在MATLAB。这似乎很容易,但我想不通,我错了。问题是:

有关MMM = 1:
1)如果m = 1时取C1 = C1B和c2 = 1-c1和用于u1,2(i)和P1,2执行循环(ⅰ)

2)在m = 2取C1 = C1 +直流和c2 = 1-C1,而这个时间执行循环与新c1和c2为u1,2(i)和P1,2(ⅰ)

3)在m = 3取C1 =(C1 * ST(1) - (C1-DC)的* st(2))/(ST(1)-st(2)),并执行循环新C1和C2。

然后增加迭代次数:MMM = 2;
嗯保持N-R迭代次数的计数。在第一次迭代具有嗯= 1,第二个嗯= 2,等等(这个特定运行仅做2次迭代)。
sumint是积分的内部。

我需要在code绘制这些数字,但MATLAB列出了错误。请帮我。

在code的有关部分:

  II = 101;
   U =细胞(2,1);
ini_cond = [0,0];
对于i = 1:2;
   ü{I} =零(1,II);
   ü{I}(:, II)= ini_cond(I)*兰特(1,1);
结束
对于i = 1:二;
        fikness = FIK *罪(PI * X);
        ü{1}(I)=(C1-H1D *(X-0.5)+ AD / 2 *(X-0.5)^ 2)./(H1-0.5 * fikness-A *(X-0.5)) ;
        ü{2}(I)=(C2 + H1D *(X-0.5)-AD / 2 *(X-0.5)^ 2)./(1-H1 + 0.5 * fikness + A *(X- 0.5));
结束p值=细胞(2,1);
Q =细胞(2,1);
对于i = 1:2;
   p {I} =零(1,ⅱ);
   q {I} =零(1,ⅱ);
结束p {1}(1)= 0.5 *(1-U {1}(1)^ 2);
q {1}(1)= 0;
p {2}(1)= 0.5 *(1-U {2}(1)^ 2);
q {2}(1)= 0;对于i = 2:101q {1}(ⅰ)= Q {1}(I-1)-dx *(U {1}(I-1)-UB {1}(ⅰ-1))./ dt的;
p {1}(ⅰ)= 0.5 *(1-U {1}(ⅰ)^ 2)+ Q {1}(ⅰ);
q {2}(ⅰ)= Q {2}(I-1)-dx *(U {2}(I-1)-UB {2}(I-1))./ dt的;
p {2}(ⅰ)= 0.5 *(1-U {2}(ⅰ)^ 2)+ Q {2}(ⅰ);结束
ST =零(2,长度(t))的;
ST(1,:)= P {1}(100)-p {2}(100);
M = M + 1;如果m == 3;
C1 =(C1 * ST(1) - (C1-DC)的* st(2))/(ST(1)-st(2));
C2 = 1-C1;
结束
对于i = 1:2;
   sumint {I} =零(1,长度(t))的;
结束
sumint =细胞(2,1);
sumint {1}(1)= 0.5 *(对{2}(1)-p {1}(1));
sumint {2}(1)= 0.5 *(对{2}(1)-p {1}(1))*( - 1/2)。对于i = 2:II-1;
X =(I-1)* DX;
sumint {1}(ⅰ)= sumint {1}(ⅰ-1)+(第{2}(ⅰ)-p {1}(ⅰ));
sumint {2}(ⅰ)= sumint {2}(I-1)+(第{2}(ⅰ)-p {1}(ⅰ))*(X-1/2);结束H1DDOT = -sumint {1} * dx./rmass。
H1D = H1D + DT * H1DDOT;
H1 = H1 + DT * H1D;
ADDOT = sumint {2} * DX / rmomi。
AD = AD + DT * ADDOT;
A = A + DT * AD;H1L = H1 + A. * 0.5;
H1R = H1-A * 0.5。
H2 = 1-H1;
RAT1 = AD / ADinit。
RAT2 = ADDOT / AD。

妳的速度p与pressures C1,C2被该外倾角影响H1DDOT和ADDOT是H1和A. SUM1和SUM2的第二推导被积分的内部以限定H1DDOT和ADDOT的值。 H1DDOT和ADDOT是时间的函数。


解决方案

你可以从消息看,错误的是这一行:

  sumint {2}(ⅰ)= sumint {2}(I-1)+(第{2}(ⅰ)-p {1}(ⅰ))。*( X-1/2);

现在,让我们来找出原因:

  sumint {2}(I)= ...

这部分意味着你要插入无论是在右侧的 I 个细胞阵列的位置 sumint {2} 。这意味着,在右侧必须是一个标量。

是吗?

好吧, sumint {2}(I-1)+(P {2}(我)-p {1}(I))肯定是一个标量,因为你使用单个值作为索引所有矢量/阵列。问题是乘法 *(X-1/2);

从上面的code,很显然, X 是一个矢量/阵列,(因为你使用长度(X)等)。乘以标 sumint {2}(I-1)+(P {2}(我)-p {1}(I)),由矢量 X ,会给你回一个载体,其中提到,将无法正常工作。

也许你想在 I 的值x

有其他几个奇怪的事情在你的code,持续了例如:

 对于i = 1:101;
        fikness = FIK *罪(PI * X);
        ü{1} =(C1-H1D *(X-0.5)+ AD / 2 *(X-0.5)^ 2。)./(H1-0.5 * fikness-A *(X-0.5));
        ü{2} =(C2 + H1D *(X-0.5)-AD / 2 *(X-0.5)^ 2)./(1-H1 + 0.5 * fikness + A *(X-0.5)) ;
结束

为什么你有一个循环吗?你是不是循环什么,你都在做同样的计算100倍。再次,我想这应该是 X(I)

更新:

引入您的新的错误,因为你改变了 X 成一个标量。然后 U =零(1,长度(X))只会是标量,这意味着ü{1}(我)将在失败我〜= 1

I need to iterate Newton-Raphson in MATLAB. It seems easy but I cannot figure out where I am wrong. The problem is:

For mmm=1: 1) If m=1 take c1=c1b and c2=1-c1 and do the loop for u1,2(i) and p1,2(i)

2)If m=2 take c1=c1+dc and c2=1-c1, and this time do the loop with new c1 and c2 for u1,2(i) and p1,2(i)

3) If m=3 take c1=(c1*st(1)-(c1-dc)*st(2))/(st(1)-st(2)) and do the loop for new c1 and c2.

Then increase the iteration number: mmm=2 ; mmm keeps count of the number of N-R iterations. The first iteration has mmm=1, the second mmm=2, etc. (This particular run only do 2 iterations). sumint are inside of the integrals.

I need to plot these figures in the code but MATLAB gives errors below. Please help me.

Relevant part of the code:

ii=101;
   u = cell(2, 1);
ini_cond = [0,0];
for i = 1:2;
   u{i} = zeros(1,ii);
   u{i}(:, ii) = ini_cond(i) * rand(1, 1);
end    


for i=1:ii;
        fikness=fik*sin(pi.*x);
        u{1}(i)=(c1-H1D*(x-0.5)+AD/2.*(x-0.5).^2)./(H1-0.5*fikness-A*(x-0.5));
        u{2}(i)=(c2+H1D*(x-0.5)-AD/2.*(x-0.5).^2)./(1.-H1+0.5*fikness+A*(x-0.5)); 
end

p = cell(2, 1);
q = cell(2, 1);


for i = 1:2;
   p{i} = zeros(1,ii);
   q{i} = zeros(1,ii);
end    

p{1}(1)=0.5*(1.-u{1}(1).^2);
q{1}(1)=0;
p{2}(1)=0.5*(1.-u{2}(1).^2);
q{2}(1)=0;

for i=2:101

q{1}(i)=q{1}(i-1)-dx*(u{1}(i-1)-ub{1}(i-1))./dt;
p{1}(i)=0.5*(1.-u{1}(i).^2)+q{1}(i);
q{2}(i)=q{2}(i-1)-dx*(u{2}(i-1)-ub{2}(i-1))./dt;
p{2}(i)=0.5*(1.-u{2}(i).^2)+q{2}(i);

end


st = zeros(2, length(t));
st(1,:)=p{1}(100)-p{2}(100);
m=m+1;

if m==3; 
c1=(c1*st(1)-(c1-dc)*st(2))/(st(1)-st(2));
c2=1-c1;
end
for i = 1:2;
   sumint{i} = zeros(1,length(t));
end 
sumint = cell(2, 1);
sumint{1}(1)=0.5*(p{2}(1)-p{1}(1));
sumint{2}(1)=0.5*(p{2}(1)-p{1}(1)).*(-1/2);

for i=2:ii-1;
x=(i-1)*dx;
sumint{1}(i)=sumint{1}(i-1)+(p{2}(i)-p{1}(i));
sumint{2}(i)=sumint{2}(i-1)+(p{2}(i)-p{1}(i))*(x-1/2);

end

H1DDOT=-sumint{1}.*dx./rmass;
H1D=H1D+dt*H1DDOT;
H1=H1+dt*H1D;
ADDOT=sumint{2}*dx./rmomi;
AD=AD+dt*ADDOT;
A=A+dt*AD;

H1L=H1+A.*0.5;
H1R=H1-A.*0.5;
H2=1.-H1;
rat1=AD./ADinit;
rat2=ADDOT./AD;

u are the velocities p are the pressures c1,c2 are the camber effects H1DDOT and ADDOT are the second derivation of H1 and A. sum1 and sum2 are the inside of the integrals to define the values of H1DDOT and ADDOT. H1DDOT and ADDOT are functions of time.

解决方案

As you can see from the message, the error is with this line:

sumint{2}(i)=sumint{2}(i-1)+(p{2}(i)-p{1}(i)).*(x-1/2);

Now, let's find out why:

sumint{2}(i) = ...

This part means you want to insert whatever is on the right side to the ith position of the array in cell sumint{2}. That means, the right side must be a scalar.

Is it?

Well, sumint{2}(i-1)+(p{2}(i)-p{1}(i)) is certainly a scalar, since you use a single value as index to all the vectors/arrays. The problem is the multiplication .*(x-1/2);.

From the code above, it is clear that x is a vector / array, (since you use length(x) etc). Multiplying the scalar sumint{2}(i-1)+(p{2}(i)-p{1}(i)), by the vector x, will give you back a vector, which as mentioned, will not work.

Maybe you want the ith value of x?

There are several other strange things going on in your code, for instance:

for i=1:101;
        fikness=fik*sin(pi.*x);
        u{1}=(c1-H1D*(x-0.5)+AD/2.*(x-0.5).^2)./(H1-0.5*fikness-A*(x-0.5));
        u{2}=(c2+H1D*(x-0.5)-AD/2.*(x-0.5).^2)./(1.-H1+0.5*fikness+A*(x-0.5)); 
end

Why do you have a loop here? You are not looping anything, you are doing the same calculations 100 times. Again, I guess it should be x(i).

Update:

Your new error is introduced because you changed x into a scalar. Then u = zeros(1,length(x)) will only be a scalar, meaning u{1}(i) will fail for i ~= 1.

这篇关于如何改变价值观,做功能牛顿迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆