MATLAB Answers

Summing up two graphs with different starting point

3 views (last 30 days)
Hey Matlab Community,
I have two graphs as seen from the figure below and I need to sum them up.
For the graph I have 4 separate column vectors. So for Red I have two separate column vectors with X and Y values and similarly for Blue I have two separate X and Y values The challenge is they both start from different point. The Red one starts at 25C and the Blue one starts at 170C. How can I plot the sum of graphs ? ( Just to clarify the resulting graph should be exactly equal to red graph until 170C and then it would start to deviate because of the addition of Blue.)
I hope I managed to explain the challenge I am facing. Any help would be greatly appreciated.
Thanks and Greetings
Kush

Accepted Answer

ANKUR KUMAR
ANKUR KUMAR on 16 Mar 2021
Edited: ANKUR KUMAR on 16 Mar 2021
Method 1: Using boolean
clc
clear
YY=rand(151,2);
XX=[[50:5:800]' [175:5:925]']
data=[XX(:,1) YY(:,1) XX(:,2) YY(:,2)]
plot(data(:,1), data(:,2),'k');
hold on
plot(data(:,3), data(:,4),'b');
uniq=unique([data(:,1);data(:,3)]);
for kk=1:length(uniq)
index1=find(data(:,1)==uniq(kk));
if ~isempty(index1)
value1(kk)=data(index1,2);
else
value1(kk)=nan;
end
index2=find(data(:,3)==uniq(kk))
if ~isempty(index2)
value2(kk)=data(index2,4);
else
value2(kk)=nan;
end
end
hold on
plot(uniq,value1+value2,'r')
Method 2: Interpolation
clc
clear
% generating random data
YY=rand(151,2);
XX=[[50:5:800]' [175:5:925]']
data=[XX(:,1) YY(:,1) XX(:,2) YY(:,2)]
plot(data(:,1), data(:,2),'k');
hold on
plot(data(:,3), data(:,4),'b');
uniq=unique([data(:,1);data(:,3)]);
value1=interp1(data(:,1),data(:,2),uniq);
value2=interp1(data(:,3),data(:,4),uniq);
plot(uniq,value1+value2,'r')
  1 Comment
Kushagra Mehrotra
Kushagra Mehrotra on 16 Mar 2021
Ohh wow, this is incredible !
Thank you so much for taking time out and writing a code to help me, you are a saviour ! I will try to run the code and Accept the answer ASAP. Again thanks for the time you took to formulate that !

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by