# 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

6 views (last 30 days)
Alex Doan on 28 Feb 2020
Answered: Walter Roberson on 28 Feb 2020
%%
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
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
percent_error = 0;
while percent_error<=0.1 || percent_error>=10
percent_error = input('Enter the percent difference to compute [0.1% - 10%]: ');
end
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);
F= ["Yes","No"];
again = menu('Repeat newton rsphson calculation?',F);
if again == 2
end
end

the cyclist on 28 Feb 2020
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.
Alex Doan on 28 Feb 2020
Can you help?

Walter Roberson on 28 Feb 2020
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.