MATLAB Answers

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
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
%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 Comments

the cyclist
the cyclist on 28 Feb 2020
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.

Sign in to comment.

Answers (1)

Walter Roberson
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.

  0 Comments

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!