我如何让它进行多次迭代,直到满足某个标准?

发布于 2025-01-17 06:11:09 字数 1254 浏览 1 评论 0原文

我已经为克拉默规则编写了一个脚本,我需要进行多次迭代,直到它满足设定的标准?我正在处理的问题是,我要求它使用初始假设,然后一旦检索到答案,就在矩阵中再次使用该答案并重复该方法,直到它满足我提出的任何标准,并显示完成的迭代量。我对八度或一般编程相对较新。我看过各种教科书,但似乎没有给我一个清晰的例子来说明我将如何解决这个问题

所以下面的代码是我写的,它按照我想要的方式工作,但它不会自动执行另一次迭代,因为我不知道如何将其放入八度音阶,而是选择多次复制相同的代码,对其进行微小的更改,我相信有一种更简单的方法可以做到这一点?

z=[A*(Q1.^0.852) B*(Q2.^0.852) C*(Q3.^0.852); 1 -1 0; 0 1 -1];
p=[0;4.32;3.28];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
D=z
b=p;
N=length(b);
Di=zeros(N,1);
d=det(D);
D_original = D;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (d ~=0)

  for i=1:N;
    D(:,i)=b;
    Di(i)=det(D)/d;
    D=D_original;
  endfor
printf("Iteration No: 1_____________________________________________________\n");

printf("solution is:\n");
Q1x=Di(1,1)
Q2x=Di(2,1)
Q3x=Di(3,1)
printf("____________________________________________________________________\n");
printf("criterion check(percentage):\n");
Q1e1=abs(((abs(Q1x)-abs(Q1))/abs((Q1x))))*100 
Q2e2=abs(((abs(Q2x)-abs(Q2))/abs((Q2x))))*100 
Q3e3=abs(((abs(Q3x)-abs(Q3))/abs((Q3x))))*100 
printf("____________________________________________________________________\n");
else
print("Cramer's Rule not aplicable \n");

endif

I have written a script for cramer's rule which I require to do a number of iterations until it has met a set criterion? The issue I am dealing with is that I require it to use an initial assumption and then once retrieving an answer to use that answer again in the matrix and repeat that method until it has met whatever criterion I set forth an display the amount of iterations done. I am relatively new to octave or programming in general. I've looked at various text books but nothing seems to give me a clear example of how I would go about this

So the code below is what I've written and it works as I'd like it to however it does not automatically do another iteration because I do not know how to put that into octave instead I have opted to copy this same code multiple times making minor changes to it and I believe there is a simpler way to do this?

z=[A*(Q1.^0.852) B*(Q2.^0.852) C*(Q3.^0.852); 1 -1 0; 0 1 -1];
p=[0;4.32;3.28];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
D=z
b=p;
N=length(b);
Di=zeros(N,1);
d=det(D);
D_original = D;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (d ~=0)

  for i=1:N;
    D(:,i)=b;
    Di(i)=det(D)/d;
    D=D_original;
  endfor
printf("Iteration No: 1_____________________________________________________\n");

printf("solution is:\n");
Q1x=Di(1,1)
Q2x=Di(2,1)
Q3x=Di(3,1)
printf("____________________________________________________________________\n");
printf("criterion check(percentage):\n");
Q1e1=abs(((abs(Q1x)-abs(Q1))/abs((Q1x))))*100 
Q2e2=abs(((abs(Q2x)-abs(Q2))/abs((Q2x))))*100 
Q3e3=abs(((abs(Q3x)-abs(Q3))/abs((Q3x))))*100 
printf("____________________________________________________________________\n");
else
print("Cramer's Rule not aplicable \n");

endif

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文