1

I've been trying to create a quadratic graph that passes through an intersection of two linear graphs. So far I've been able to get the corner but it seems like I cannot use the intersection in \addplots. This is what I got so far.

\usepackage{tikz, pgfplots}
\usetikzlibrary{calc, intersections}

\pgfplotsset{compat=1.16}

\begin{document} \begin{tikzpicture} \begin{axis} [ xmin=0, xmax=10, ymin=0, ymax=10, domain=-10:10, ]

\addplot[color=black, name path=A] {x}; \addplot[color=black, name path=B] {-x + 10}; \addplot[name intersections={of=A and B, by=C}] coordinates {(C)};

\end{axis} \end{tikzpicture} \end{document}

It seems like there's an error in this line \addplot[name intersections={of=A and B, by=C}] coordinates {(C)}; in the last (C) as when I replace the (C) with (0,0) the plot renders fine without the intersection.

If this worked, I was planning to use the x y coordinate given by C to set the vertex of another quadratic graph with the formula a(x-v)^2+h. Is there a way to achieve this?

jun2040
  • 13
  • Welcome to TeX.SE! – Mensch Jan 07 '23 at 16:12
  • You can not easily use named coordinates in PGFPlots - see: https://tex.stackexchange.com/a/415881/8650 You have the formula for the two linear graphs, - then you should be able to find whatever other graph you need from them. – hpekristiansen Jan 07 '23 at 16:19

2 Answers2

1

Here's an alternative approach in Metapost.

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    numeric u; u = 20;
    path xx, yy, A, B, C;
    xx = (left -- 11 right) scaled u;
    yy = (down -- 11 up) scaled u;
A = (left--right) scaled 16u rotated 45;  % y = x
B = (left--right) scaled 16u rotated -45 shifted (0, 10u); % y = 10 - x
C = (-1, 1){1, -2} .. (-1/2, 1/4){1, -1} ..  (0, 0){1, 0} .. (1/2, 1/4){1, 1} .. (1, 1){1, 2};  % y = x^2
C := C scaled 10u shifted (A intersectionpoint B);

drawarrow xx;
drawarrow yy;
% now save the size of the drawing so far
interim bboxmargin := 0pt; path b; b = bbox currentpicture;

draw A withcolor 2/3 blue; 
draw B withcolor 2/3 blue; 
draw C withcolor 2/3 red;

clip currentpicture to b;

endfig; \end{mplibcode} \end{document}

I am guessing a bit what the OP actually wants, but if you compile this with lualatex you will get this:

enter image description here

For more about Metapost follow the link at the top.

Thruston
  • 42,268
0
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[
declare function={
a1=1; b1=0;
a2=-1; b2=10;
f1=a1*x+b1;
f2=a2*x+b2;
v=(b2-b1)/(a1-a2);
h=a1*v+b1;
a=0.2;
f3=a*(x-v)^2+h;
}]
\begin{axis}[
xmin=0, xmax=10,
ymin=0, ymax=10,
domain=-10:10,
]
\addplot[samples=2] {f1};
\addplot[samples=2] {f2};
\addplot[red, thick, samples=50, smooth] {f3};
\end{axis}
\end{tikzpicture}
\end{document}

Two linear graphs and a parabola