How to graph equations with different x domains?

7 views (last 30 days)
Suppose I have two equations:
x(t)= -10000e^(-0.05t) + 10000.......(1) where 0<=t<=26
x(t)= -5123.635e^(-0.05t) + 10000.......(2) where 26<=t<=100
How do graph these two equations on the same window ?
I tried to use the hold on hold off function but im failing to do it. Is there any other way to do it? Im a beginner so easy codes please ;)

Accepted Answer

Star Strider
Star Strider on 28 Apr 2021
First, code it correctly, then use logical indexing —
t = linspace(0, 110);
x1 = @(t) -10000*exp(-0.05*t) + 10000;
x2 = @(t) -5123.635*exp(-0.05*t) + 10000;
v1 = ((t>=0) & (t<=26));
v2 = ((t>=26) & (t<=100));
figure
plot(t(v1), x1(t(v1)))
hold on
plot(t(v2), x2(t(v2)))
hold off
grid
.
  4 Comments
Muhammad Faaz Usman
Muhammad Faaz Usman on 28 Apr 2021
Edited: Muhammad Faaz Usman on 28 Apr 2021
Yes it has worked. I have tweaked the code as per the question. Now i want to connect the two curves with a curved line to show the decay. How do I go about that?
t = linspace(0, 110);
x1 = @(t) -10000*exp(-0.05*t) + 10000;
x2 = @(t) -5123.635*exp(-0.05.*(t-34)) + 10000;
v1 = ((t>=0) & (t<=26));
v2 = ((t>=34) & (t<=100));
figure
plot(t(v1), x1(t(v1)))
hold on
plot(t(v2), x2(t(v2)))
hold off
grid
Star Strider
Star Strider on 28 Apr 2021
I would do something like this —
t = linspace(0, 110);
x1 = @(t) -10000*exp(-0.05*t) + 10000;
x2 = @(t) -5123.635*exp(-0.05*t) + 10000;
v1 = ((t>=0) & (t<=26));
v2 = ((t>=26) & (t<=100));
ofst = min(x2(t(v2)))-max(x1(t(v1)))
ofst = 1.4360e+03
figure
plot(t(v1), x1(t(v1)))
hold on
plot(t(v2), x2(t(v2))-ofst)
hold off
grid
That is likely as close as it is possible to match them. Even though the both have 26 in common, the small reminaing gap is caused by the resolution of ‘t’.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!