フィルターのクリア

trying to get when I say yes it would repeat again and if no it stops the loop, but it would store all the yes values until i say no im like 95% almost done with it just need that part

1 回表示 (過去 30 日間)
%%
choice= input('Enter a vector sufficent for an odd-order polynomial: ');
a=numel(choice);
C = 1;
%b = mod(a,2);
while mod(a,2)~=0
C=C+1;
choice= input('Enter a vector sufficent for an odd-order polynomial: ');
if C == 5 && mod(a,2)~=0
warning('Odd number of coefficients entered. Last element removed');
choice(:,end)=[];
end
a=numel(choice);
end
%Task 2
while again == 1
guess=input('Enter an intial guess for the Newton Ralph method: ');
derv = polyder(choice);
b= polyval(derv,guess);
D=1;
while b==0
D=D+1;
derv = polyder(choice);
b= polyval(derv,guess);
guess=input('Enter an intial guess for the Newton Ralph method: ');
if D == 3 && b==0
error('Initial guess causes divide by 0. Program terminated');
end
end
%Task 3
percent_error = 0;
while percent_error<=0.1 || percent_error>=10
percent_error = input('Enter the percent difference to compute [0.1% - 10%]: ');
end
%Task 4
xn=guess-(polyval(choice,guess)./(polyval(polyder(choice),guess)));
Counter=1;
Val = [guess,xn];
while (abs(Val(Counter+1)-Val(Counter))/Val(Counter)).*100 > percent_error
Counter=Counter+1;
xn=Val(Counter)-(polyval(choice,Val(Counter))./(polyval(polyder(choice),Val(Counter))));
Val(Counter+1) = xn;
end
fprintf('The solution converged in %d iterations',Counter);
%Task 5
F= ["Yes","No"];
again = menu('Repeat newton rsphson calculation?',F);
if again == 2
end
end
  2 件のコメント
the cyclist
the cyclist 2020 年 2 月 28 日
Responding to your email here.
I made no change to your code. I only put it into "code" format, using the formatting buttons. Maybe you pasted something incorreclty? (I supposed it is possible I inadvertently changed something, but I'm 99.9% sure I did not.)
Because it is your question, you can edit it to fix it, regardless.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 2 月 28 日
choice= input('Enter a vector sufficent for an odd-order polynomial: ');
if C == 5 && mod(a,2)~=0
As I told you in one of your previous questions, you must adjust a every time you update choice. You did not do so here: you are still working with the number of elements of the previous vector, not the number of elements of the vector that was just entered by the user.

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by