combining two different codes

2 views (last 30 days)
yogeshwari patel
yogeshwari patel on 14 Jun 2021
Commented: yogeshwari patel on 15 Jul 2021
%%%%%%%%%%%%%%%%% 1 code %%%%%%%%%%%
syms a
P1=1
P2=3
U(1)=1;
U(2)=a;
for k=1:10
B=0;
for i=1:k
B=simplify(B+U(i)*U(k-i+1));
end
U(k+2)=(P1*B+P2*U(k))/(k*(k+1));
end
for k=1:3
series(x)=simplify(series(x)+U(k)*(power(x,k-1)));
end
series
%%%%%%%%%%%%% I differentiate the series and tehn i evalaute the value of differentiate polynomial at x=1.
A=diff(series,x,1)
ans=A(1)
After that i want to evanalte the root
%%%%%%%%%%%%%%%%%%%%%%%%% 2 code%%%%%%%%%%%%%%%%
accuracy=input('enter the accuracy')
g=inline('ans-1')% root of the polynomial
a=input('enter the ist approximation=')
b=input('enter the 2nd approximation=')
fa=feval(g,a)
fb=feval(g,b)
while fa*fb>0
a=input('enter the ist approximation=')
b=input('enter the 2nd approximation=')
fa=feval(g,a)
fb=feval(g,b)
end
for i=1:100
c=(a+b)/2;
fc=feval(g,c);
disp([i a fa b fb c fc abs(b-a)])
if fc==accuracy
fprintf('the root of the equation is %f\n',c)
break;
elseif abs(b-a)<=accuracy
fprintf('the root of the equation is %f\n',c)
break;
elseif fa*fc<=0
b=c;
fb=fc;
else
a=c;
fa=fc;
end
end
when i combie the two code the ans is not correct .If I run it separtely then i get correct value so i dont know where the logic is going wrong

Answers (2)

Walter Roberson
Walter Roberson on 14 Jun 2021
do not use inline()
Use
g = matlabFunction(A)
  3 Comments
Walter Roberson
Walter Roberson on 14 Jun 2021
I guess instead of using
g = matlabFunction(A)
you should be using
g = matlabFunction(A(1)-1)

Sign in to comment.


yogeshwari patel
yogeshwari patel on 14 Jul 2021
Thanks for your ans Its working .Can you tell me why inline commond does work?
  2 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!

Translated by