How can I determine the time and corresponding values of two functions when the value one function is 20% larger than the other one with a FOR oder WHILE loop?

1 visualizzazione (ultimi 30 giorni)
Hi guys,
I'm new to Matlab and there is a task I don't know how to solve. So I have this code:
Pumax=75E3; % factor in front of exp-term
ku=0.045; % the decay rate
Pumin=1E5;
Psmax=3E5;
Po=1E4;
ks=0.08;
t = [0:1:200]
Ps=Psmax./(1+((Psmax/Po)-1)*exp(-ks*t)); %suburbs
Pu= Pumax*exp(-ku*t) + Pumin ; %city
Now i shall determine the time and corresponding values of Pu(t) and Ps(t) when the population of the suburbs are 20% larger than the city with a for or a while loop.
  1 Commento
Stephen23
Stephen23 il 23 Ott 2017
Modificato: Stephen23 il 23 Ott 2017
Note that the square brackets are not needed when creating a vector, as you are not concatenating anything. All you need is:
t = 0:1:200;
You will also notice that the MATLAB editor shows a warning saying that the square brackets are not required. See:

Accedi per commentare.

Risposte (1)

Reza Bonyadi
Reza Bonyadi il 24 Ott 2017
Modificato: Stephen23 il 24 Ott 2017
I would do the following: First, you want x such that Ps(x)=Pu(x)+0.2*Pu(x). So, define an anonymous function:
myf = @(x)(((Psmax./(1+((Psmax/Po)-1)*exp(-ks*x))))-1.2*(Pumax*exp(-ku*x) + Pumin));
This essentially defines the function myf(x)=Ps(x)-(Pu(x)+0.2*Pu(x))
You are then after an x in a way that myf(x) is 0. You can find such x using many methods, such as fzero:
fzero(myf,35)
35 is just a guess for the solution and the function returns 39.6068.
Indeed if you do
a=39.6068;Psmax./(1+((Psmax/Po)-1)*exp(-ks*a))-1.2*(Pumax*exp(-ku*a) + Pumin)
you will get close to zero.
You can see the corresponding values by:
a=39.6068;disp([(Psmax./(1+((Psmax/Po)-1)*exp(-ks*a))) Pumax*exp(-ku*a) + Pumin]);
Does that work?

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by