I need to find 3 roots of an equation (e^x=3*x^2 - transcendent equation) through the iteration method in Matlab. What algorithm should I use?

 채택된 답변

Matt J
Matt J 2015년 4월 5일
편집: Matt J 2015년 4월 5일

0 개 추천

You can use FZERO, e.g.,
>> f=@(x) exp(x)-3*x.^2
>> [xroot,res]=fzero(f,.1)
You will need to provide an initial guess of each of the three roots, which you can obtain by plotting the function f(x)=e^x-3*x^2

댓글 수: 10

paula ro
paula ro 2015년 4월 5일
I forgot to mention that I can't use functions like fzero or roots. I have to use an algorithm like Bisection, Fixed Point etc. - an iteration method Or, which algorithm is similar to fzero?
Matt J
Matt J 2015년 4월 5일
편집: Matt J 2015년 4월 5일
The documentation for fzero describes it as a combination of secant, bisection, and inverse quadratic interpolation.
Anyway, to answer your originally posted question, bisection seems like an easy and appropriate thing to apply. As you can see from the graph of f=@(x) exp(x)-3*x.^2, the function changes signs at all its roots.
paula ro
paula ro 2015년 4월 5일
From what I understand, all these alghorithms offer one root. I need to find 3 roots and for that I'll have to define 3 different intervals for 3 individual roots (not sure if this is correct).
Matt J
Matt J 2015년 4월 5일
편집: Matt J 2015년 4월 5일
Yes. As I said in my initial response, search points and intervals can be eyeballed from a plot of the function.
Could you elaborate? For this bisection alghorithm:
function [x,code]=Bisection(a,b,f,eps,n_iterations)
c=(a+b)/2;
n=1;
x=[];
while( ((b-a)>eps) &&(abs(f(c))>eps) && (n<=n_iteratii))
if (f(c)==0)
disp ('x - wrong root');
elseif( f(c)*f(b)<0 )
a=c;
elseif ( f(a)*f(c)<0 )
b=c;
else
disp('We stopped at interval (a,b)');
return
end
n=n+1;
c=(a+b)/2;
x=[x,c];
end
if((b-a)<=eps && (abs(f(c))<=eps))
code=0;
elseif ((b-a)<=eps)
code=1;
elseif ((abs(f(c))<=eps))
code=2;
else % maximum iterations
code=3; % didn't get the expected root
end
axis([a-1, b+1, f(a)-10,f(b)+10]);
x_init=a-1:0.1:b+1; %
hold on
plot(a,f(a),'or');plot(b,f(b),'or'); %draw interval
plot(x_init,f(x_init),'b'); %draw initial function
%representing the graph with the points of x
plot(x,f(x),'pk');
hold off
end
What exactly should I do? How can I add two more intervals? Everytime I change the interval, I get different numbers of results (guesses?)
No, you manually call the function 3 times specifying a different [a,b] each time (taken from the plot) for each of the solutions you are looking for. For example:
f=@(x) exp(x)-3*x.^2;
x1=Bisection(-.6,-.2, f,eps,n_iterations);
x2=Bisection( 0.8 ,1, f,eps,n_iterations);
x3=Bisection( 3.8, 4, f,eps,n_iterations);
paula ro
paula ro 2015년 4월 7일
If I insert the Command Window "Bisection(0,4,@f,10^-4,20)", I get this result:
What interval should I insert next? Or this plot is wrong
If I plot
f=@(x) exp(x)-3*x.^2
on the interval [-5,5], I can see all 3 roots.
paula ro
paula ro 2015년 4월 7일
Actually my function is 3*x^2-exp(x). I don't understand how do you see all 3 roots. My graph shows one: 3.6-3.8. How exactly do I choose the next interval?
James Tursa
James Tursa 2015년 4월 8일
편집: James Tursa 2015년 4월 8일
f = @(x) 3*x.^2-exp(x)
x = -5:.01:5;
plot(x,f(x))
grid on
All three roots are on the plot.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

paula ro
paula ro 2015년 4월 8일

0 개 추천

Ok. But I need an iteration algorithm that finds these 3 roots without me manually extracting the intervals from the plot. The equation has multiple roots. I called the function 3 times with 'Bisection' and it just plots one root 0.9 and displays 'We stopped at interval (a,b)' Is there an algorithm that finds these 3 roots in a function? I only found for polynomials.

댓글 수: 2

James Tursa
James Tursa 2015년 4월 8일
Sounds like you are asking quite a lot from this algorithm. Determining good starting guesses for an arbitrary function is not at all trivial. Even determining just how many roots there are is not trivial. You are certainly not going to get some simple code on this forum that does this for you for an arbitrary function. Seems like there is going to have to be some manual work from you up front for doing this for any particular function you are interested in.
Matt J
Matt J 2015년 4월 9일
편집: Matt J 2015년 4월 9일
But I need an iteration algorithm that finds these 3 roots without me manually extracting the intervals from the plot.
You've wasted a lot of time by concealing that requirement. I advised you to find the 3 roots in this manual way in my very first response and in several subsequent comments. You didn't even blink.
As James says, though, there is no method for finding all roots of an arbitrary function. One reason that this is impossible is because some functions have infinite roots, arbitrarily close together, even on a finite interval. Examples are f(x)=0 or f(x)=sin(1/x)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

질문:

2015년 4월 5일

편집:

2015년 4월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by