How can I plot an equation in the form y=mx+c
Show older comments
I want to plot an equation where t1=p*t2+g , i have already defined what p and g are and both t1 and t2 are vectors of size 8x1, i tried ezplot('p*t2+g') but i get errors including saying t2 isn't defined, something about inline/feval and other malarkey. tbh im new to matlab so any help wouldn't go amiss, thanks.
Answers (2)
Star Strider
on 28 Nov 2015
If you already have ‘t1’ and ‘t2’, you can plot them directly:
figure(1)
plot(t2, t1)
grid
7 Comments
HI2
on 28 Nov 2015
Image Analyst
on 28 Nov 2015
On what figure? Is there another figure you didn't tell us about? Attach your code. It's saying t2 isn't defined, despite what you think. Actually I'm surprised you say you already plotted it if t2 is not defined and you got an error message. How could that (plotting successfully) happen????
Star Strider
on 28 Nov 2015
That wasn’t obvious in your original post.
Use a text object:
text(mean(t2), mean(t1), sprintf('t_2 = %f\\cdott_1 %+f', p, g))
That will work, but you may want to tweak it just a bit to put it where you want. (I did not specifically test this code.)
Image Analyst
on 28 Nov 2015
Husnain's so called "Answer" moved here since it's not an answer to the original question at the very top:
This is the code I've used to plot the graphs. I'm still have errors with ezplot appearing.
T1a=T1(1:8,4)
T2a=T2(1:8,4)
plot(T2a,T1a)
h=ezplot('T1a=p*T2a+g');
Star Strider
on 28 Nov 2015
According to your code, ‘T1’ and ‘T2’ (neither of which we have) are matrices of at least 8 rows and at least 4 columns, and ‘T1a’ and ‘T2a’ are 8-element column vectors derived from them.
I cannot do anything with the code you posted.
You can plot your function with ezplot if you create an anonymous function from it:
p = 2;
g = 3;
h=ezplot(@(T2a) p*T2a+g);
HI2
on 28 Nov 2015
Star Strider
on 28 Nov 2015
You have to present it to ezplot as an ‘anonymous function’ as I did (see the documentation for ezplot for details), along with correct values of ‘p’ and ‘g’ and the correct range of values (minimum and maximum) for ‘T2a’.
Image Analyst
on 28 Nov 2015
0 votes
You know that MATLAB is case sensitive, don't you? T2 is different than t2. You have to use the same capitalization if you want them to be the same variable.
I never use ezplot() - I just define t2 so I know what it will be rather than trust that function to select a range that I may or may not be happy with.
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!