Search code examples
matlabplotoctaveequation-solving

Solving equation and getting answer where two plots meet


I have two plots I created in matlab / octave I would like them to cross at x=4 or any other point on x I choose. How can I go about doing this in matlab or octave?

Example code:

x = linspace(0,2*pi,1000);
y = 1./exp(x); % 
%subplot(2,1,1); 
plot(x,y,'r')
title('e(x)')
hold on

y2 = -y+.09; % 
%subplot(2,1,2); 
plot(x,y2,'b')

enter image description here Thanks


Solution

  • If you change your second equation to be

    y2 = -y+c
    

    Then

    1/exp(x) = -1/exp(x) + c
    

    thus

    c = 2/exp(x)
    

    so just choose what the x-value of the intersection should be, plug it in that formula and that's your c. So for a crossing at x==4, c=2/exp(x) which is 0.036631 so

    y2 = -y + 0.036631
    

    So (assuming you can alter c) all you need to choose the x-vlaue of the point of intersection is to put that desired x-value into the equation

    c = 2/exp(x)     %//note this is a scalar value of x, not the whole vector