MATLAB Finding Difference of two equations

992 Views Asked by At

So I'm trying to figure out how to do a fairly simply mathematical task in MATLAB but I don't know what it is called so I don't know what to search. Basically what I want to do is if you have two equations, say: $$(1) \text{ }y=3t+2$$ $$(2) \text{ } y=\frac{7}{2}t+2$$

No I want to find the difference between the two graphs at each point $t$. So at $t=1$ the difference between the two would be $\frac{1}{2}$ and at $t=2$ the difference is $1$.

So basically I want MATLAB to make a table of difference between the two graphs at every discrete value of $t$ in a specified range of $t$ values. My equations are way more complicated than this (it's the Lorenz attractor to be specific). Any ideas on how to do this?

1

There are 1 best solutions below

0
On BEST ANSWER

You can declare new function either as a difference of two other functions or use anonymous function.

function test
clc
clear all
x = -pi:0.01:pi;
subplot(1,2,1)
plot(x, func1(x), x, func2(x))
hold on
plot(x, func3(x), 'r-', 'LineWidth', 2)
title('Using standard function definition')

subplot(1,2,2)
plot(x, func1(x), x, func2(x))
hold on
func4 = @(x) func1(x) - func2(x);
plot(x, func4(x), 'r-', 'LineWidth', 2)
title('Using anonymous function')

function val = func1(x)
val = sin(x);

function val = func2(x)
val = sin(2*x);

function val = func3(x)
val = func1(x) - func2(x);