MATLAB - "MATLAB® combines a desktop environment tuned for iterative analysis and design processes with a programming language that expresses matrix and array mathematics directly." Which I have started to use as a quick and dirty programming language as well as making cool animations so here are some of the things I have used MATLAB for.
Purpose - While learning MATLAB in ENGE 1215, a required course for first year engineering students at Virginia Tech, I was experimenting with the sphere command in MATLAB. I then put it in a loop to gradually increase the number of faces and thought it looked cool so I then made a program that will record that progress.
clear
loop = 2000;
v = VideoWriter('sphears2000.avi');
h = figure;
v.FrameRate = 60;
v.Quality = 100;
%disp(v)
open(v);
h.Visible = 'off';
for i = 1:loop
sphere(i);
axis equal
title(i)
%drawnow
%disp(getframe)
disp((i/loop) * 100)
writeVideo(v, getframe(h));
end
h.Visible = 'on';
close(v);
Goes to 500 faces
Goes to 2000 Faces
Up to 100 Faces Images
Purpose - The purpose was to experiment with the limits of MATLAB in variable size and how much it can export to Excel.
clear
clc
format long G
startTime = datetime('now');
row = 6.5e4;
%row = 6.506e3;
col = row;
random = rand(row, col);
disp("Outputing")
%writematrix(random,"Good_Luck2.xlsx")
sum = sum(random, 'all');
disp(sum)
count = row * col;
disp(count)
average = mean(random, 'all');
disp(average)
endTime = datetime('now');
totalTime = between(startTime, endTime);
disp(totalTime)
The results were that MATLAB seems to store variables in RAM with the max export to Excel being around a 6.506e3 squared matrix and the max variable being around a 8.43e4 square matrix for 32 GB of RAM. I also learned that MATLAB will use your RAM until your computer crashes due to no RAM.
Purpose - The purpose is to calculate Pi to large decimal places using different methods. Those methods are Gregory Leibniz Series, Nilakantha, Random Numbers , and Arctan. Then comparing how quickly they converge on to Pi.
clear
clc
clf
format long G
iterations = 3000;
digits(5000)
% greg's infinite series
denomenator = 1;
gregPi = vpa(0);
%Nilakantha's infinite series
num = 2;
nilPi = vpa(3);
%ran pi
coPrime = 0;
ranPi = 0;
randMax = 2^53 - 1;
%formula using arctan
arc5 = 0;
odd = 1;
number5 = vpa(((1/5)^odd) / odd);
arc239 = 0;
number239 = vpa(((1/239)^odd) / odd);
for i = 1:iterations
% greg's infinite series
if mod(i, 2) == 0
gregPi = gregPi + (4/denomenator);
else
gregPi = gregPi - (4/denomenator);
end
gregy(i) = percentError(abs(gregPi));
denomenator = denomenator + 2;
%formula using arctan
if(mod(i, 2) == 0)
arc5 = arc5 - number5;
arc239 = arc239 - number239;
else
arc5 = arc5 + number5;
arc239 = arc239 + number239;
end
odd = odd + 2;
number5 = ((1/5)^odd) / odd;
number239 = ((1/239)^odd) / odd;
x(i) = i;
arcy(i) = percentError(vpa(4*((4*abs(arc5))- abs(arc239))));
piey(i) = [vpa(pi)];
%Nilakantha's infinite series
if mod(i - 1, 2) == 0
nilPi = nilPi + (vpa(4/(num*(num+1)*(num+2))));
else
nilPi = nilPi - (vpa(4/(num*(num+1)*(num+2))));
end
nily(i) = percentError(nilPi);
num = num + 2;
%ran pi
if(iscoprime([randi([1, randMax]), randi([1,randMax])]))
coPrime = coPrime + 1;
end
if(coPrime > 0)
rany(i) = percentError(sqrt(6/(coPrime/(i + 1))));
end
end
hold on
start = 1;
%plot(x, piey, 'LineWidth', 2)
plot(x(start:end), arcy(start:end))
plot(x(start:end), gregy(start:end))
plot(x(start:end), nily(start:end))
plot(x(start:end), rany(start:end))
legend('ArcTan', 'Greg', 'Nil', 'Random')
%ylim([0, 0.5])
disp('Done')
function y = percentError(num)
y = abs((num - vpa(pi)) / vpa(pi)) * 100;
end
1000 Iterations
2000 Iterations
3000 Iterations
Purpose - In the Spring 2023 semester I started learning Latex to be able to type my math notes and homework's. The problem was that to my knowledge their was no perfect way to convert a PDF to a TEX file thus I made the documents from scratch. The thing was that I then needed a way to reverse engineer the given graphs on my notes or homework's so that I can recreate them. The solution I found was by using Lagrange Interpolating Polynomial which is a way to input a amount of points and get a polynomial that intersects those points. The problem was that online converters would simplify to a traditional polynomial like 1.56x^2 + 6.7x + 0.5 which involved rounding which I didn't want. Thus I made a script in MATLAB that can take a vector of x points and a vector of y points and will return a unsimplified polynomial formula that intersects those points.
clear all
close all
clc
x = [8 10];
y = [2 -2];
n = length(x);
P = "";
for i = 1:n
P = P + "((" + y(i) + "*";
for j = 1:n
if(j ~= i)
if (x(j) > 0)
P = P + "(x-" + x(j) + ")*";
else
P = P + "(x+" + abs(x(j)) + ")*";
end
end
end
P = extractBefore(P, strlength(P));
P = P + ")/";
temp = 1;
for j = 1:n
if(j ~= i)
temp = temp * (x(i) - x(j));
end
end
P = P + temp + ")" + "+";
end
P = extractBefore(P, strlength(P));
disp(P);
Points: (0, 0), (5, 2) and (6, 3)
Formula Output: ((0*(x-5)*(x-6))/30)+((2*(x+0)*(x-6))/-5)+((3*(x+0)*(x-5))/6)
Simplified: 1/2 (x - 5) x - 2/5 (x - 6) x
Original
From Scratch
\documentclass[tikz, 12pt]{article}
\usepackage[margin=0.7in]{geometry}
\usepackage{amsmath}
\usepackage[makeroom]{cancel}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{ stmaryrd }
\usepackage{multicol}
\usepackage{tasks}
\usepackage{tabularx}
\usepackage{tasks}
\usepackage{vwcol}
\usepackage{booktabs}
\usepackage{tabularray}
\usepackage{pgfplots}
\usepackage{fancyhdr}
\usepackage{xcolor}
\usepackage{soul}
\usepgflibrary{plotmarks}
\def\newline{\hfill \break}
\def\({\left(}
\def\){\right)}
\def\ddx{\frac{d}{dx}}
\def\[{\left[}
\def\]{\right]}
\def\ddy{\frac{dy}{dx}}
\setul{0.5ex}{0.2ex}
\setulcolor{red}
\begin{document}
\begin{flushleft}
\begin{large}\textbf{Section 4.1: Minimum and Maximum Values (Part a)}\end{large} \\
\newline
In this section we will learn how to use the derivative of a function to find its maximum and
minimum values. \\
Furthermore, we will learn conditions which guarantee that a function has extreme values, and
methods for determining their location. \\
\newline
\textbf{Warm Up} \\
\newline
Suppose the graph of a function $f$ below. What is the \ul{highest} and \ul{lowest} values of $f$. \\
\newline
\begin{center}
\begin{tikzpicture}
\begin{axis} [
width = 0.5\textwidth,
axis x line=middle,
axis y line=middle,
ymin = -1, ymax = 6,
xmin = -4.5, xmax = 5.5,
xtick={-4, -3, -2, -1, 0, 1, 2, 3, 4, 5
},
ytick = {-1, 0, 1, 2, 3, 4, 5},
xlabel = {$x$},
ylabel = {$y$},
every axis plot/.append style={ultra thick},
clip = false,
axis line style = {<->, thick},
]
\addplot [
black,
samples = 200,
domain = -3:1,
]
{((5*(x+3)*(x+0)*(x-1))/4)+((2*(x+1)*(x+0)*(x-1))/-24)+((4*(x+1)*(x+3)*(x-1))/-3)+((1*(x+1)*(x+3)*(x+0))/8)};
\addplot [
blue,
samples = 200,
domain = -1.5:-.5,
]
{5};
\addplot [
black,
samples = 200,
domain = 1:4,
]
{((1*(x-1.5)*(x-2.25)*(x-4))/-1.875)+((2*(x-1)*(x-2.25)*(x-4))/0.9375)+((3*(x-1)*(x-1.5)*(x-4))/-1.6406)+((4*(x-1)*(x-1.5)*(x-2.25))/13.125)} node[right]{$y = f(x)$};
\addplot [red, dotted] coordinates {(-3, 0) (-3, 2)};
\addplot [red, samples = 200, domain = -3:0, dotted]{2};
\addplot [red, dotted] coordinates {(-1, 0) (-1, 5)};
\addplot [red, samples = 200, domain = -1:0, dotted]{5};
\addplot [red, dotted] coordinates {(1, 0) (1, 1)};
\addplot [red, samples = 200, domain = 0:1, dotted]{1};
\addplot [red, dotted] coordinates {(4, 0) (4, 4)};
\addplot [red, samples = 200, domain = 0:4, dotted]{4};
\end{axis}
\end{tikzpicture}
\end{center}
\begin{flalign*}
\text{The Highest} &= \text{Largest Value} & f(-1) &= 5 \text{ The largest value of } f\\
\text{The Lowest} &= \text{Smallest Value} & f(1) &= 1 \text{ The smallest value of } f
\end{flalign*}
$$\text{Domain }f\text{:} [-3,4]$$ \\
\begin{flalign*}
\text{The maximum value is } 5 \text{ at } x &= -1 \\
\text{The minimum value is } 1 \text{ at } x &= 1
\end{flalign*}
\fbox{
\parbox{\textwidth}{
\newline
\textbf{Definition} \\
\newline
Let \ul{$c$} be a number \ul{in the domain $D$} of a function $f$.
\begin{itemize}
\item{If $f(c) \ge f(x)$ for all $x$ in $D$, then $f(c)$ is an \textbf{\ul{absolute maximum}} (or \textbf{\ul{global maximum}}) value.}
\item{If \color{red}\fbox{\color{black}$f(c) \le f(x)$} \color{black} for all $x$ in $D$, then $f(c)$ is an \textbf{\ul{absolute minimum}} (or \textbf{\ul{global minimum}}) value.}
\end{itemize}
\newline
The minimum and maximum values of $f$ are known as \textbf{\ul{extreme values}} or just \textbf{extrema}.
}}
\clearpage
How do we find absolute extrema? \\
\vspace{4ex}
\begin{tblr}{
width=\textwidth,
rows = {halign = c, valign = m}, colspec={X X},}
{\begin{tikzpicture}
\begin{axis} [
width = \linewidth,
axis x line=middle,
axis y line=middle,
ymin = -1, ymax = 3,
xmin = -2, xmax = 2,
xtick={-1, 0, 1},
ytick = {0, 1, 2},
xlabel = {$x$},
ylabel = {$y$},
every axis plot post/.append style={ultra thick},
clip = false,
axis line style = {<->, thick},
title = {$f(x) = x^2 + 1 \text{ on } \(-\infty, \infty\)$},
]
\addplot [
black,
samples = 200,
domain = -1:1,
style={<->}
]
{x^2 + 1};
\addplot[mark=*] coordinates {(0,1)};
\addplot [
red,
dotted,
samples = 200,
domain = -0.5:0.5,
]
{1} node[right]{$f'(0) = 0$};
\node[red, thick] (source) at (axis cs:-1.5,0.5){abs min is $1$ at $x=0$};
\node (destination) at (axis cs:0, 1){};
\draw[red, thick, ->](source)--(destination);
\end{axis}
\end{tikzpicture} \\
There is no abs max} &
{\begin{tikzpicture}
\begin{axis} [
width = \linewidth,
axis x line=middle,
axis y line=middle,
ymin = -1, ymax = 3,
xmin = -2, xmax = 2,
xtick={-1, 0, 1},
ytick = {0, 1, 2},
xlabel = {$x$},
ylabel = {$y$},
every axis plot post/.append style={ultra thick},
clip = false,
axis line style = {<->, thick},
title = {$f(x) = x^2 + 1 \text{ on } \[0, 1\]$},
]
\addplot [
black,
samples = 200,
domain = 0:1,
]
{x^2 + 1};
\addplot[mark=*] coordinates {(0,1)};
\addplot[mark=*] coordinates {(1,2)};
\addplot [
red,
dotted,
samples = 200,
domain = 0:1,
]
{2};
\node[red, thick] (source) at (axis cs:-1.5,0.5){abs min is $1$ at $x=0$};
\node (destination) at (axis cs:0, 1){};
\draw[red, thick, ->](source)--(destination);
\node[red, thick] (source) at (axis cs:2,3){abs max is $2$ at $x=1$};
\node (destination) at (axis cs:1, 2){};
\draw[red, thick, ->](source)--(destination);
\addplot [red, dotted] coordinates {(1, 0) (1, 2)};
\end{axis}
\end{tikzpicture} \\
The abs max and min occur at the endpoints} \\
\end{tblr} \\
\vspace{10ex}
\begin{tblr}{
width=\textwidth,
rows = {halign = c, valign = m}, colspec={X X}}
{\begin{tikzpicture}
\begin{axis} [
width = \linewidth,
axis x line=middle,
axis y line=middle,
ymin = -1, ymax = 3,
xmin = -2, xmax = 2,
xtick={-1, 0, 1},
ytick = {0, 1, 2},
xlabel = {$x$},
ylabel = {$y$},
every axis plot post/.append style={ultra thick},
clip = false,
axis line style = {<->, thick},
title = {$f(x) = x^2 + 1 \text{ on } \( 0, 1\]$},
]
\addplot [
black,
samples = 200,
domain = 0:1
]
{x^2 + 1};
\fill[white,draw=black, very thick](axis cs:0,1)circle[radius=3pt];
\addplot[mark=*] coordinates {(1,2)};
\addplot [
red,
dotted,
samples = 200,
domain = 0:1,
]
{2};
\node[red, thick] (source) at (axis cs:-1.5,0.5){No abs min};
\node (destination) at (axis cs:0, 1){};
\draw[red, thick, ->](source)--(destination);
\node[red, thick] (source) at (axis cs:2,3){abs max is $2$ at $x=1$};
\node (destination) at (axis cs:1, 2){};
\draw[red, thick, ->](source)--(destination);
\addplot [red, dotted] coordinates {(1, 0) (1, 2)};
\end{axis}
\end{tikzpicture}} &
{\begin{tikzpicture}
\begin{axis} [
width = \linewidth,
axis x line=middle,
axis y line=middle,
ymin = -1, ymax = 3,
xmin = -2, xmax = 2,
xtick={-1, 0, 1},
ytick = {0, 1, 2},
xlabel = {$x$},
ylabel = {$y$},
every axis plot post/.append style={ultra thick},
clip = false,
axis line style = {<->, thick},
title = {$f(x) = x^2 + 1 \text{ on } \(0, 1\)$},
]
\addplot [
black,
samples = 200,
domain = 0:1,
]
{x^2 + 1};
\fill[white,draw=black, very thick](axis cs:0,1)circle[radius=3pt];
\addplot [
red,
dotted,
samples = 200,
domain = 0:1,
]
{2};
\node[red, thick] (source) at (axis cs:-1.5,0.5){No abs min};
\node (destination) at (axis cs:0, 1){};
\draw[red, thick, ->](source)--(destination);
\node[red, thick] (source) at (axis cs:2,3){No abs max};
\node (destination) at (axis cs:1, 2){};
\draw[red, thick, ->](source)--(destination);
\addplot [red, dotted] coordinates {(1, 0) (1, 2)};
\fill[white,draw=black, very thick](axis cs:1,2)circle[radius=3pt];
\end{axis}
\end{tikzpicture}} \\
\end{tblr}
\clearpage
\fbox{
\parbox{\textwidth}{
\newline
\textbf{Definition} \\
\newline
Let \ul{$c$ be a number} in the domain $D$ of a function $f$.
\begin{itemize}
\item{The number $f(c)$ is a \textbf{\ul{local minimum}} (or \textbf{\ul{relative minimum}}) value of $f$ if \\
\color{red}\fbox{\color{black}$f(c) \le f(x)$} \color{black} for \ul{all $x$} in an \ul{open interval containing $c$}}
\item{The number $f(c)$ is a \textbf{\ul{local maximum}} (or \textbf{\ul{relative maximum}}) value of $f$ if \\
\color{red}\fbox{\color{black}$f(c) \ge f(x)$} \color{black} for \ul{all $x$} in an \ul{open interval containing $c$}}
\end{itemize}
Local maxima/minima are sometimes referred to as \textbf{local extrema}.
}} \\
\newline
\textbf{Note:} Following along with Stewart’s convention, \ul{local extrema will only occur on the
\textit{interior} of an interval}, and \ul{never at the endpoints}. However, \ul{endpoints can be absolute
extrema}. \\
\newline
How do we find local extrema? \\
\newline
The $x$ value where $f'(c) = 0$ or $f'(c)$ Does not exist (corner, cusp) \\
\newline
\textbf{Example 1} \\
\newline
Use the graph to state the \ul{absolute} and \ul{local maximum and minimum} values of the
function. \\
\end{flushleft}
\begin{center}
\newline
\begin{tikzpicture}
\begin{axis} [
width = 0.9\textwidth,
axis x line=middle,
axis y line=middle,
ymin = -4.5, ymax = 6.5,
xmin = -7.5, xmax = 7.5,
xtick={-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7},
ytick = {-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6},
xlabel = {$x$},
ylabel = {$y$},
every axis plot post/.append style={ultra thick},
clip = false,
axis line style = {<->, thick}
]
\addplot[mark=*] coordinates {(0,0)};
\addplot [
black,
samples = 200,
domain = -7:-6,
]
{((-3*(x+6))/-1)+((1*(x+7))/1)};
\node[red, thick] (source) at (axis cs:-8,-4){\fbox{abs min}};
\node (destination) at (axis cs:-7, -3){};
\draw[red, thick, ->](source)--(destination);
\addplot [red, dotted] coordinates {(-7, 0) (-7, -3)};
\addplot[red, dotted, domain = -7:0]{-3};
\node[red, thick] (source) at (axis cs:-7,2){Loc max};
\node (destination) at (axis cs:-6, 1){};
\draw[red, thick, ->](source)--(destination);
\addplot [red, dotted] coordinates {(-6, 0) (-6, 1)};
\addplot[red, dotted, domain = -6:0]{1};
\addplot [
black,
samples = 200,
domain = -6:-5,
]
{((-2*(x+6)*(x+5.5))/0.5)+((1*(x+5)*(x+5.5))/0.5)+((-1*(x+5)*(x+6))/-0.25)};
\node[red, thick] (source) at (axis cs:-4,-2.5){Loc min};
\node (destination) at (axis cs:-5, -2){};
\draw[red, thick, ->](source)--(destination);
\addplot [red, dotted] coordinates {(-5, 0) (-5, -2)};
\addplot[red, dotted, domain = -5:0]{-2};
\node[red, thick] (source) at (axis cs:-5.5, 3){Loc max};
\node (destination) at (axis cs:-4.5, 2){};
\draw[red, thick, ->](source)--(destination);
\addplot [red, dotted] coordinates {(-4.5, 0) (-4.5, 2)};
\addplot[red, dotted, domain = -4.5:0]{2};
\addplot [
black,
samples = 200,
domain = -5:-4,
]
{((-2*(x+4.5)*(x+4)*(x+4.25))/-0.375)+((2*(x+5)*(x+4)*(x+4.25))/0.0625)+((1*(x+5)*(x+4.5)*(x+4.25))/0.125)+((1.5*(x+5)*(x+4.5)*(x+4))/-0.046875)};
\node[red, thick] (source) at (axis cs:-3, -1){Loc min};
\node (destination) at (axis cs:-4, 1){};
\draw[red, thick, ->](source)--(destination);
\addplot [red, dotted] coordinates {(-4, 0) (-4, 1)};
\addplot [
black,
samples = 200,
domain = -4:-2,
]
{((1*(x+3.5)*(x+3)*(x+2)*(x+2.25))/1.75)+((3*(x+4)*(x+3)*(x+2)*(x+2.25))/-0.46875)+((3.5*(x+4)*(x+3.5)*(x+2)*(x+2.25))/0.375)+((2.5*(x+4)*(x+3.5)*(x+3)*(x+2.25))/0.75)+((3*(x+4)*(x+3.5)*(x+3)*(x+2))/-0.41016)};
\node[red, thick] (source) at (axis cs:-4, 4.5){Loc max};
\node (destination) at (axis cs:-3, 3.5){};
\draw[red, thick, ->](source)--(destination);
\addplot [red, dotted] coordinates {(-3, 0) (-3, 3.5)};
\addplot[red, dotted, domain = -3:0]{3.5};
\addplot [
black,
samples = 200,
domain = -2:-1,
]
{((2.5*(x+1)*(x+1.75))/0.25)+((1*(x+2)*(x+1.75))/0.75)+((2*(x+2)*(x+1))/-0.1875)};
\addplot [
black,
samples = 200,
domain = -1:-0,
]
{((1*(x+0))/-1)+((0*(x+1))/1)};
\addplot [
black,
samples = 200,
domain = 0:2,
]
{((5*(x+0)*(x-1))/2)+((0*(x-2)*(x-1))/2)+((3.75*(x-2)*(x+0))/-1)};
\node[red, thick] (source) at (axis cs:3, 6){\fbox{Loc max and abs max}};
\node (destination) at (axis cs:2, 5){};
\draw[red, thick, ->](source)--(destination);
\addplot [red, dotted] coordinates {(2, 0) (2, 5)};
\addplot[red, dotted, domain = 0:2]{5};
\addplot [
black,
samples = 200,
domain = 2:5,
]
{((5*(x-2.5)*(x-4)*(x-4.75)*(x-5))/8.25)+((4*(x-2)*(x-4)*(x-4.75)*(x-5))/-4.2188)+((3*(x-2)*(x-2.5)*(x-4.75)*(x-5))/2.25)+((2.5*(x-2)*(x-2.5)*(x-4)*(x-5))/-1.1602)+((2*(x-2)*(x-2.5)*(x-4)*(x-4.75))/1.875)};
\node[black, thick] at (axis cs:6,3){\large$y=f(x)$};
\addplot [red, dotted] coordinates {(6, 0) (6, 0.5)};
\addplot[red, dotted, domain = 0:6]{0.5};
\addplot [
black,
samples = 200,
domain = 5:6,
]
{((0.5*(x-5))/1)+((2*(x-6))/-1)};
\end{axis}
\end{tikzpicture}
\end{center}
\pagebreak
\begin{flushleft}
\textbf{Example 2} \\
\newline
For each of the following, determine whether \ul{such a function exists}. If so, provide an example.
\begin{enumerate}
\item{ A \ul{continuous function} defined for \ul{all real numbers} \ul{with no absolute extrema}. That is, a continuous function with no absolute maximum and no absolute minimum. \\
\newline
\begin{tblr}{
width=\textwidth,
rows = {halign = c, valign = m}, colspec={X Q[c, m] X}}
{\begin{tikzpicture}
\begin{axis} [
width = 0.5\linewidth,
ticks=none,
axis x line=middle,
axis y line=middle,
ymin = -8, ymax = 8,
xmin = -2, xmax = 2,
xlabel = {$x$},
ylabel = {$y$},
every axis plot post/.append style={ultra thick, <->},
clip = false,
axis line style = {<->, thick},
title = {$f(x) = x^3$},
]
\addplot [
black,
samples = 200,
domain = -2:2
]
{x^3};
\end{axis}
\end{tikzpicture}} & or &
{\begin{tikzpicture}
\begin{axis} [
width = 0.5\linewidth,
ticks=none,
axis x line=middle,
axis y line=middle,
ymin = -2, ymax = 2,
xmin = -2, xmax = 2,
xlabel = {$x$},
ylabel = {$y$},
every axis plot post/.append style={ultra thick, <->},
clip = false,
axis line style = {<->, thick},
title = {$f(x) = x$},
]
\addplot [
black,
samples = 200,
domain = -2:2,
]
{x};
\end{axis}
\end{tikzpicture}} \\
\end{tblr} \begin{center} No abs max and no abs min \end{center}}
\item{A \ul{function} defined on the closed \ul{interval $[0, 1]$} with no \ul{absolute maximum} and \ul{no absolute minimum}. \\
\begin{center}
\begin{tikzpicture}
\begin{axis} [
width = 0.25\linewidth,
axis x line=middle,
axis y line=middle,
ymin = -1, ymax = 1,
xmin = -1, xmax = 1,
xtick = {0, 1},
ytick = {1/2, 1},
xlabel = {$x$},
ylabel = {$y$},
every axis plot post/.append style={ultra thick},
clip = false,
axis line style = {<->, thick},
title = {{$\displaystyle{f(x)=\begin{cases} \frac{1}{2}, & x=0, x=1 \\ x, & 0<x<1\end{cases}}$}},
]
\addplot [
black,
samples = 200,
domain = 0:1,
]
{x};
\fill[white,draw=black, very thick](axis cs:0,0)circle[radius=3pt];
\fill[white,draw=black, very thick](axis cs:1,1)circle[radius=3pt];
\addplot[mark=*] coordinates {(0,1/2)};
\addplot[mark=*] coordinates {(1,1/2)};
\end{axis}
\end{tikzpicture} \\
No abs max and no abs min \end{center}}
\item{ A \ul{\textbf{continuous} function} defined on the \ul{closed interval $[0, 1]$} with no \ul{absolute maximum} and \ul{no absolute minimum}. \\
\newline
Impossible to find continuous function defined in close interval without abs max and abs min \\
\begin{center}
\begin{tikzpicture}
\begin{axis} [
width = 0.25\linewidth,
axis x line=middle,
axis y line=middle,
ymin = -1, ymax = 3,
xmin = -1, xmax = 7*pi / 2,
xtick = {pi/2, 7*pi/2},
xlabel = {$x$},
ylabel = {$y$},
every axis plot post/.append style={ultra thick},
clip = false,
axis line style = {<->, thick},
xticklabels={$a$, $b$}
]
\addplot [
black,
samples = 200,
domain = pi/2:7*pi / 2,
]
{cos(deg(x+pi/2))+2};
\addplot [red, dotted] coordinates {(pi/2, 0) (pi/2, 1)};
\addplot[red, dotted, domain = 0:pi/2]{1};
\addplot [red, dotted] coordinates {(7 *pi/2, 0) (7 *pi/2, 3)};
\addplot[red, dotted, domain = 0:7 * pi/2]{3};
\addplot[blue, domain = 3 * pi/2 - 1:3 * pi/2 + 1]{3};
\addplot[blue, domain = 5 * pi/2 - 1:5 * pi/2 + 1]{1};
\end{axis}
\end{tikzpicture}
\end{center}
}
\end{enumerate}
\fbox{
\parbox{\textwidth}{
\newline
\textbf{The Extreme Value Theorem} \\
\newline
If $f$ is \ul{continuous} on a closed interval $[a, b]$, then $f$ attains an \ul{absolute maximum value
$f(c)$} and an \ul{absolute minimum value $f(d)$} at\ul{ some numbers $c$ and $d$ in $[a, b]$}.\\}} \\
\newline
\textbf{Note: } The EVT does \textbf{NOT} say where these values are located.
\end{flushleft}
\end{document}
Purpose - When we were learning about Riemann Sums in Calculus 1 we did problems like expressing the area of a function on some interval to a limit of a Riemann Sum and converting a limit of a Riemann Sum to a definite Integral and I wanted to check my work. Thus I made a MATLAB script that you can input the inner function of a summation and how many numbers to sum and it will output the sum. The goal was to calculate the summation and compare that against a caluator for the derivied integral.
clear
clc
n = 10^6;
sum = 0
for i = 1:n
sum = sum + sums(i, n);
end
disp(sum)
function x = sums(i, n)
x = (5/n)*sqrt(1+((5*i)/n));
end
Purpose - I wanted to use the knowledge gained from the Sphere Animation to make a animation of adding a amount of rectangles to a function in order to approximate the integral of the function. In other words to animate a Riemann Sum approximation continually getting closer.
clear
clc
clf
close all
format long
%Rieman Sum Variables
a = 1;
b = exp(1);
n = 100;
x = linspace(a,b,100);
fun = @(x) log(x);
y = fun(x);
h = figure;
exact = integral(fun, a, b, 'RelTol',0,'AbsTol', 1e-12);
%Video Variables
name = func2str(fun);
writer = VideoWriter(name + " " + n + '.avi');
writer.FrameRate = floor(n / 10);
writer.Quality = 100;
open(writer);
writeVideo(writer, getframe(h));
hold on
for i = 1:n
clf
plot(x,y, 'LineWidth', 1);
sum = 0;
for j = 1:i
title(name + " Rectangles: " + i + " Estimation: " + sum + " Exact: " + exact);
deltaX = (b - a) / i;
x2 = a + j*deltaX;
x1 = x2 - deltaX;
height = fun(x1);
if (height < 0)
r = rectangle('Position',[x1, height, deltaX, abs(height)], ...
'FaceColor','b');
else
r = rectangle('Position',[x1, 0, deltaX, height], ...
'FaceColor','r');
end
if (i >= 500)
r.LineStyle = 'none';
end
sum = sum + (deltaX * height);
uistack(r,'bottom')
end
writeVideo(writer, getframe(h));
disp ((i / n) * 100);
end
close(writer);
disp('Done');
cos(x) to 100 Rectangles
ln(x) to 100 Rectangles
sin(x) to 100 Rectangles
sqrt(x) to 100 Rectangles
y = x to 100 Rectangles
cos(x) to 1000 Rectangles
Purpose - While taking Calculus 2 we reviewed Riemann Sums with the heights at different points and some problems asked for a sketch. However I didn't want to make a inaccurate drawing so I instead made a MATLAB script to take a function and a vector of points. The script will then plot the function and also plot the rectangles at the given values height. I latter added another vector which is intended to plot the maximum Riemann Sum approximation and the minimum Riemann Sum approximation. The script also outputs the summation of the Riemann Sum.
clear
clc
clf
close all
format long
func = @(x) (1+x.^2);
P = [1 1.5 2 3 4 5];
c = [1 1.5 2 3 4];
d = [1.5 2 3 4 5];
a = P(1);
b = P(length(P));
x = linspace(a,b,1000);
y = func(x);
lineWidth = 10;
plot(x, y, "Color", "black", "LineWidth", lineWidth);
hold on
axis = gca;
axis.FontSize = 50;
%for i = 1:length(c)
% plot([c(i) c(i)], [0 func(c(i))], ...
% "Color", "red", "LineWidth", lineWidth);
%end
sum = 0;
for i=1:length(P) - 1
x1 = P(i);
x2 = P(i + 1);
deltaX = x2 - x1;
height = func(c(i));
sum = sum + (deltaX * height);
r = rectangle('Position',[x1, 0, deltaX, abs(height)], ...
'FaceColor','b');
uistack(r,'bottom')
end
disp(sum);
sum = 0;
for i=1:length(P) - 1
x1 = P(i);
x2 = P(i + 1);
deltaX = x2 - x1;
height = func(d(i));
sum = sum + (deltaX * height);
r = rectangle('Position',[x1, 0, deltaX, abs(height)], ...
'FaceColor','r');
uistack(r,'bottom')
end
disp(sum);
Inputs:
f(x) = 1 + x^2
P = {1, 1.5, 2, 3, 4, 5}, this is the x values of the rectangles and is used to find width
c = {1, 1.5, 2, 3, 4}, this is used to find height with c being for the minimum and blue
d = {1.5, 2, 3, 4, 5}, this is used to find height with d being for the maximum and red
Outputs:
34.625000000000000, the sum for c
57.125000000000000, the sum for d
Purpose - When doing a problem for Calculus 2 I wanted to get more decimal places on where two functions intersected so I made a MATLAB script to use Newtons Method to approximate the zero.
clear
clc
clf
close all
format longg
syms f(x)
f(x) = ((75*x) - x^3 - 125);
dxfunc = diff(f, x);
x_n = 2;
n = 6;
for i = 1:n
num = f(x_n);
den = dxfunc(x_n);
temp = x_n;
x_n = vpa(x_n - (num / den));
if(temp == x_n)
disp("Lost Percision at n = " + i)
break;
end
end
disp(x_n)
Where 75x - x^3 = 125 which can be rewritten as 75x - x^3 - 125 = 0 to be able to use Newtons Method.
Inputs:
f(x) = ((75*x) - x^3 - 125), the function to find the zero of
x_n = 2, this is the initial guess for Newtons Method
n = 1000, how many iterations, more will be more accurate
Outputs:
Lost Percision at n = 6
1.7364817766693034885171662676931
Purpose - When doing a problem in Calculus 2 we were doing problems involving using integrals to find the volume of a solid. To aid me in solving one problem I created a MATLAB script aiming to recreate the photo on the left but with the function the problem asked.
clear
clc
clf
close all
format long
syms f(x)
f(x) = 8 - x^2;
a = 0;
b = sqrt(8);
h = b;
x = linspace(a,b,1000);
y = f(x);
hold on;
view(3)
plot3(zeros(length(x), 1), x, y, 'r');
plot3(y, x, zeros(length(x), 1), 'b')
set(gca, 'ydir', 'reverse')
set(gca, 'xdir', 'reverse')
xlabel('x')
ylabel('y')
zlabel('z')
p1 = [0 h/2 f(h/2)];
p2 = [0 h/2 0];
p3 = [f(h/2) h/2 0];
p4 = [f(h/2) h/2 f(h/2)];
rectangle = fill3([p1(1) p2(1) p3(1) p4(1) p1(1)], [p1(2) p2(2) p3(2) p4(2) p1(2)], ...
[p1(3) p2(3) p3(3) p4(3) p1(3)], 'y');
view(-75,26)
f(x) = 8 - x^2, the function to graph
a = 0, the starting point
b = sqrt(8), the ending point
Purpose - In Calculus 2 there was a homework problem that produced a integral that I couldn't solve, with the book's answer even saying that it used a calculator to solve the problem. However I wanted to get a answer that I came up with instead of saying that a calculator solved it so I made a MATLAB proggram that uses Simpson's Rule to approximate what a integral evaluates too.
clear
clc
clf
close all
format long
syms f(x)
f(x) = sqrt(1 + cos(x)^2);
a = 0;
b = pi / 4;
n = 100000;
deltaX = (b - a) / n;
sum = 0;
alt = 4;
for i = 1:(n + 1)
x = vpa(f(a + (deltaX * (i - 1))));
if(i == 1 || i == (n + 1))
alt = 1;
end
sum = sum + (alt * x);
if (alt == 4)
alt = 2;
else
alt = 4;
end
end
disp(vpa((deltaX / 3) * sum));
Performance Information
Example
Inputs:
f(x) = sqrt(1 + cos(x)^2), the function in the integral
a = 0, the lower bound
b = pi / 4, the upper bound
n = 100000, how many numbers to add, more is more accurate
Output:
1.0580955013925628679166237942681
Purpose - I was experimenting with the code to make it quicker and realized that you can input a vector into a symbolic function which got me to learn that if you work with vectors and matrixes in MATLAB they are generally faster. I then began remaking the code to work more with vectors and ultimately removed for loops entirely.
tic
clear
clc
clf
close all
format long
syms f(x)
f(x) = sqrt(1 + cos(x)^2);
func = @(x) sqrt(1 + cos(x).^2);
a = 0;
b = pi / 4;
n = 1000000;
deltaX = (b - a) / n;
tic
y = vpa(f(linspace(a + deltaX, b - deltaX, n - 1)));
disp("Got y")
toc
total = f(a) + f(b);
tic
odd = 4 .* y(1:2:end);
even = 2 .* y(2:2:end);
disp("Got odds and evens")
toc
tic
total = total + sum(odd) + sum(even);
disp("Got Total")
toc
disp(vpa((deltaX / 3) * total));
disp(vpa(integral(func, a, b)))
disp("Done")
toc
Performance Information
Example
Inputs:
f(x) = sqrt(1 + cos(x)^2), the function in the integral
func = @(x) sqrt(1 + cos(x).^2), the function in the integral, only used for the exact integral
a = 0, the lower bound
b = pi / 4, the upper bound
n = 100000, how many numbers to add, more is more accurate
Outputs:
Got y
Elapsed time is 10.805889 seconds.
Got odds and evens
Elapsed time is 43.808281 seconds.
Got Total
Elapsed time is 0.429525 seconds.
1.0580955013925628579868097086583
1.0580955013925630314730597092421
Done
Elapsed time is 0.485520 seconds.
Purpose - Having now made a faster version I now wanted to see how much faster it is. To do this I made a MATLAB proggram that would test each algorithm from n = 1 to n = 1000 and record how long they take and graph the difference.
clear
clc
clf
close all
format long
syms f(x)
f(x) = sqrt(1 + cos(x)^2);
a = 0;
b = pi / 4;
n = 1000;
x = 1:1:n;
version1Total = [];
version2Total = [];
for i = 1:n
version1Function = @() version1(f, a, b, i);
version1Total = [version1Total timeit(version1Function)];
version2Function = @() version2(f, a, b, i);
version2Total = [version2Total timeit(version2Function)];
end
hold on
scatter(x, version1Total, 'b', 'filled')
scatter(x, version2Total, 'r', 'filled')
xlabel('Value of n')
ylabel('Time (s)')
xFit = linspace(min(x), max(x), 1000);
coefficients1 = polyfit(x, version1Total, 1);
yFit1 = polyval(coefficients1 , xFit);
coefficients2 = polyfit(x, version2Total, 1);
yFit2 = polyval(coefficients2 , xFit);
plot(xFit, yFit1, 'b');
plot(xFit, yFit2, 'r');
version1Legend = coefficients1(1) + "x +" + coefficients1(2);
version2Legend = coefficients2(1) + "x +" + coefficients2(2);
legend("Version 1", "Version 2", version1Legend, version2Legend)
function h = version1(f, a, b, n)
deltaX = (b - a) / n;
sum = 0;
alt = 4;
for i = 1:(n + 1)
x = vpa(f(a + (deltaX * (i - 1))));
if(i == 1 || i == (n + 1))
alt = 1;
end
sum = sum + (alt * x);
if (alt == 4)
alt = 2;
else
alt = 4;
end
end
h = vpa((deltaX / 3) * sum);
end
function h = version2(f, a, b, n)
deltaX = (b - a) / n;
y = vpa(f(linspace(a + deltaX, b - deltaX, n - 1)));
total = f(a) + f(b);
odd = 4 .* y(1:2:end);
even = 2 .* y(2:2:end);
total = total + sum(odd) + sum(even);
h = vpa((deltaX / 3) * total);
end
MATLAB's Graph
Performance Information
Purpose - In Calculus 2 I was originally using Desmos to estimate what a infinite sum converges too by calculating what the sum equals for a large number, ex. 10^6. However Desmos would slow down and need a refresh to start working again so I made a MATLAB script that would do said sum.
clear
clc
clf
close all
format longg
decimals = 14;
digits(decimals)
syms a_n(x)
a_n(x) = ((-1)^(x+1) * ((1+sqrt(3*x)) / (x + 2)));
n = 1;
max = 10^6;
x = a_n(linspace(n, max, max));
sum = sum(x);
disp(vpa(sum))
disp(0.43554155527698)
Inputs:
decimals = 14, How many decimals to display
a_n(x) = ((-1)^(x+1) * ((1+sqrt(3*x)) / (x + 2))), the function to sum
n = 1, lower bound
max = 10^6, upper bound
Outputs:
0.46249844417429
0.43554155527698