Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
matlab iteration

Subject: matlab iteration

From: Safa

Date: 25 Mar, 2011 01:41:04

Message: 1 of 5

I am trying to solve the following equation iteratively in Matlab:
y=x*(1-x)^1.39-(b)
where b is a constant.

So for example:
a=800;
b=0.104;
x=0;
dx=0.0001;
y=-1;
while y<0 && x < 1 && a>500;
         x=x+dx;
         y=x*(1-x)^1.39-(b);
 end
 if (x > 1);
     disp('x is over 1');
 end
disp(['root ' num2str(x)])

Code seems to work up to certain values of b. If I put however b=0.93 for example, the code stops working. Would appreciate any suggestions to improve this code.
Many thanks
Safa

Subject: matlab iteration

From: Roger Stafford

Date: 25 Mar, 2011 02:16:05

Message: 2 of 5

"Safa" wrote in message <imgrrg$lai$1@fred.mathworks.com>...
> I am trying to solve the following equation iteratively in Matlab:
> y=x*(1-x)^1.39-(b)
> where b is a constant.
>
> So for example:
> a=800;
> b=0.104;
> x=0;
> dx=0.0001;
> y=-1;
> while y<0 && x < 1 && a>500;
> x=x+dx;
> y=x*(1-x)^1.39-(b);
> end
> if (x > 1);
> disp('x is over 1');
> end
> disp(['root ' num2str(x)])
>
> Code seems to work up to certain values of b. If I put however b=0.93 for example, the code stops working. Would appreciate any suggestions to improve this code.
> Many thanks
> Safa
- - - - - - - - - -
  That's easy to explain. Your expression x*(1-x)^1.39 attains a maximum value of 0.19697 at x = .4184 and then begins to descend. If b is set greater than this, you will never see a positive y.

  You can find this out for yourself by taking the derivative of x*(1-x)^2.39 and determining when it becomes zero in order to find this maximum value and where it occurs. This should also tell you that your code is finding only one root even though for smaller b there will actually be two roots between 0 and 1.

Roger Stafford

Subject: matlab iteration

From: Safa

Date: 25 Mar, 2011 02:32:06

Message: 3 of 5

"Safa" wrote in message <imgrrg$lai$1@fred.mathworks.com>...
> I am trying to solve the following equation iteratively in Matlab:
> y=x*(1-x)^1.39-(b)
> where b is a constant.
>
> So for example:
> a=800;
> b=0.104;
> x=0;
> dx=0.0001;
> y=-1;
> while y<0 && x < 1 && a>500;
> x=x+dx;
> y=x*(1-x)^1.39-(b);
> end
> if (x > 1);
> disp('x is over 1');
> end
> disp(['root ' num2str(x)])
>
> Code seems to work up to certain values of b. If I put however b=0.93 for example, the code stops working. Would appreciate any suggestions to improve this code.
> Many thanks
> Safa

Thanks for your quick reply. I am actually trying to solve the equation x*(1-x)^1.39-(b)=0. Well doesn't have to be exactly zero, can be equal to 10^-5 for example. So I try to vary x iteratively between 0 and 1 to find the values that would satisfy this equation. Do you suggest a better way to do this in Matlab? Again would appreciate your feedback.

Subject: matlab iteration

From: Roger Stafford

Date: 25 Mar, 2011 02:32:06

Message: 4 of 5

"Roger Stafford" wrote in message <imgtt5$ms8$1@fred.mathworks.com>...
> That's easy to explain. Your expression x*(1-x)^1.39 attains a maximum value of 0.19697 at x = .4184 and then begins to descend. If b is set greater than this, you will never see a positive y.
> ......
- - - - - - - - -
   I made a mistake in my arithmetic. The maximum is correct but occurs at x = 0.5819 .

Roger Stafford

Subject: matlab iteration

From: Roger Stafford

Date: 25 Mar, 2011 02:44:04

Message: 5 of 5

"Safa" wrote in message <imgur5$87n$1@fred.mathworks.com>...
> Thanks for your quick reply. I am actually trying to solve the equation x*(1-x)^1.39-(b)=0. Well doesn't have to be exactly zero, can be equal to 10^-5 for example. So I try to vary x iteratively between 0 and 1 to find the values that would satisfy this equation. Do you suggest a better way to do this in Matlab? Again would appreciate your feedback.
- - - - - - - - -
  Use matlab's 'fzero' function. By adjusting the 'x0' estimate parameter appropriately you can find both roots for b's that are below the maximum I mentioned.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us