2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

octaveで連立方程式を解く

Posted at

中1レベルの連立方程式

(問題)
りんご2個とみかん8個を買うと代金は880円です。
また、りんご3個とみかん5個を買うと代金は760円になります。
りんごとみかん、それぞれ1個ずつの値段はいくらでしょう。
$$
\begin{cases}
2x+8y=880 \
3x+5y=760 \
\end{cases}
$$

x=[0:200];
y=[0:200];

# 2*x + 8*y = 880
y1 = (880 - 2*x) / 8;
# 3*x + 5*y = 760
y2 =(760 -  3*x) / 5; 

plot(x,y1, x,y2)

output_3_0.png

$$
\left[\begin{matrix}
2 & 8 \
3 & 5 \
\end{matrix}\right] \quad
\left[\begin{matrix}
x \
y \
\end{matrix}\right] \quad
=
\left[\begin{matrix}
880 \
760 \
\end{matrix}\right] \quad
$$


$$
\left[\begin{matrix}
x \
y \
\end{matrix}\right] \quad
=
\left[\begin{matrix}
2 & 8 \
3 & 5 \
\end{matrix}\right] ^{-1} \quad
\left[\begin{matrix}
880 \
760 \
\end{matrix}\right] \quad
$$

A = [2 8;
     3 5];
z = [880 ;760 ];
inv(A)*z
ans =

   120.000
    80.000

3行の簡潔なコードでりんごとみかんの値段が割り出されるのは気持ちいいですね

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?