how to plot a function

조회 수: 7 (최근 30일)
Ali Salem
Ali Salem 2021년 4월 20일
편집: Johannes Hougaard 2021년 4월 20일
How can I plot this function below?
function [ content ] = caffeine( time, caffeineContent, durationBetween )
halfLife = 5.7;
tau = halfLife/log(2);
n = floor(time/durationBetween);
content = caffeineContent * exp(-time/tau) * (1-exp(durationBetween * (n+1)/tau))/(1-exp(durationBetween/tau));
end

답변 (2개)

Rik
Rik 2021년 4월 20일
편집: Rik 2021년 4월 20일
The same as any other function: find a vector of x data and calculate the y data. Then you can use plot. You can use plot3 for xyz data.
Edit:
time=linspace(0,60,200);
caffeineContent=0.5;
durationBetween=1;
content=zeros(size(time));
for n=1:numel(time)
content(n) = caffeine( time(n), caffeineContent, durationBetween );
end
plot(time,content)
function content = caffeine( time, caffeineContent, durationBetween )
halfLife = 5.7;
tau = halfLife/log(2);
n = floor(time/durationBetween);
content = caffeineContent * exp(-time/tau) * (1-exp(durationBetween * (n+1)/tau))/(1-exp(durationBetween/tau));
end
  댓글 수: 2
Ali Salem
Ali Salem 2021년 4월 20일
can you show me what that would look like?
Johannes Hougaard
Johannes Hougaard 2021년 4월 20일
It is shown in the answer

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


Johannes Hougaard
Johannes Hougaard 2021년 4월 20일
편집: Johannes Hougaard 2021년 4월 20일
Another way to do it would be using the fplot by which you don't need a for loop and to define the number of points in your x-axis
fh = @(x)caffeine(x,0.4,10); % That would be a .4 caffeineContent every 10 units.
figure; fplot(fh,[0 120]);
And I'd encourage you to vectorize your code using .* in stead of *
function content = caffeine( time, caffeineContent, durationBetween )
halfLife = 5.7;
tau = halfLife/log(2);
n = floor(time/durationBetween);
content = caffeineContent .* exp(-time/tau) .* (1-exp(durationBetween .* (n+1)/tau))/(1-exp(durationBetween/tau));
That way you could simply call
figure; plot(0:120,caffeine(0:120,0.6,8));

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by