B =
symbolic substitution error and convert
Show older comments
syms x
series1(x)=sym(zeros(1));
series2(x)=sym(zeros(1));
U=zeros(1,2,'sym');
syms x m z
alpha=15;
gamma=0.01;
U(1)=m;
U(2)=0;
A(1)=zeros(1,'sym');
for k=1
U(k+2)=z;
A(1)=0;
for m=1:k
A(1)=A(1)+U(m)*(k-m+1)*(k-m+2)*U(k-m+3);
end
B=k*(k+1)*U(k+2)+alpha*A(1)-gamma*U(k)
D=simplify(solve(B,z))
U(k+2)=D
end
disp(U(3))
for k=1:3
series1(x)=simplify(series1(x)+U(k)*(power(x,k-1)));
end
series1
e1=subs(series1,x,1);
e=e1-1;
format long
accuracy=input('enter the accuracy')
f=e(x)
g=inline(f)
g=inline(f)
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:50
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',c)
break;
elseif abs(b-a)<=accuracy
fprintf('the root of the equation is %f',c)
break;
elseif fa*fc<=0
b=c;
fb=fc;
else
a=c;
fa=fc;
end
end
fprintf('the value of c=%f', c)
series2(x)=subs(series1,m,c)
toc
the synatx is correct but still the value of m is not substituted in
4 Comments
Walter Roberson
on 18 Jan 2026
In this context, you should be avoiding inline() and feval() . You should either be proceeding symbolically, or else you should be using matlabFunction()
It is unlikely that you really want to input() inside the loop; your should be calculating a and b instead.
if fc==accuracy
Your code is confused there. First off, you have used exact comparison of binary floating point numbers: those are unlikely to happen to match bit-for-bit. Secondly, suppose the exact root was -0.05 and accuracy was 0.10 and fc happened to come out exactly as 0.10, then you would compare the fc to the accuracy, find an exact match, and declare yourself satisfied, even though fc was not within 0.10 accuracy of -0.05 . You should just get rid of that branch of code; your next line about abs(b-a) is good enough.
Walter Roberson
on 18 Jan 2026
We cannot test your code without knowing the exact values you are providing for the input() statements.
yogeshwari patel
on 18 Jan 2026
Walter Roberson
on 18 Jan 2026
You are saying that you enter 0.99999 in response to
accuracy=input('enter the accuracy')
and as well in response to each of the
a=input('enter the ist approximation=')
b=input('enter the 2nd approximation=')
???
Accepted Answer
More Answers (0)
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!