combining two different codes
3 次查看(过去 30 天)
显示 更早的评论
%%%%%%%%%%%%%%%%% 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
0 个评论
回答(2 个)
Walter Roberson
2021-6-14
do not use inline()
Use
g = matlabFunction(A)
3 个评论
Walter Roberson
2021-6-14
Sorry, we are not able to assist you in solving the task using ans-1
Consider the datatype of ans .
If ans is numeric, then you can subtract 1 from it, but you get back something that is numeric, and inline() of something numeric is just going to be that numeric value every time, which is not something you can find the root of (unless ans-1 just happened to be exactly 0)
If ans is character vector then ans-1 is a vector of double, one location for each character in the vector. This is the same situation as for ans being numeric... you cannot optimize it.
If ans is function handle, or cell array, or table, or struct, then you cannot subtract 1 from it.
If ans is an OOP object, then whether you can subtract 1 from it depends on the methods defined for the object. Most objects do not permit subtracting a value. Some objects do, such as transfer functions tf(). But finding the root of such an object is dubious.
If ans is a symbolic expression or symbolic function, then you can generally subtract 1 from it. But inline() requires taking char() of the expression, and char() of a symbolic expression or symbolic function is a result that is in a computer language that is not MATLAB and is not the internal symbolic computing language MuPAD. inline() of a symbolic function or symbolic expression can do anything from create error messages to give you an unexpected result, or on rare occasions it might do what you want to do.
Now... when you create an inline() expression, the character vector is recorded, and the character vector is eval()'d inside a generated script that sets up parameters. But... you have no control over what ans will happen to be at the time of evaluation!!
You should pretty much forget that inline() exists until you have considerably more experience with MATLAB.
Walter Roberson
2021-6-14
I guess instead of using
g = matlabFunction(A)
you should be using
g = matlabFunction(A(1)-1)
yogeshwari patel
2021-7-14
2 个评论
Walter Roberson
2021-7-14
Have you ever had a cat? Have you ever casually told the cat to do something, and the cat proceeded to do what you said? If you had a cat for long enough, probably happened every once in a while. And it probably didn't happen very often... just sometimes. You probably can't explain why it sometimes worked.
Using inline() is like telling a cat to do something: sometimes it works, but only on the simplest of situations.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!