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 (최근 30일)
이전 댓글 표시
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
Stephen23
2017년 10월 23일
편집: Stephen23
2017년 10월 23일
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:
답변 (1개)
Reza Bonyadi
2017년 10월 24일
편집: Stephen23
2017년 10월 24일
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?
댓글 수: 1
Stephen23
2017년 10월 24일
Edit: changed incorrect reference to "inline function" to correct reference to "anonymous function", with hyperlink.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!