> For the complete documentation index, see [llms.txt](https://matlab.theditor.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://matlab.theditor.xyz/everything-matlab-almost.md).

# everything matlab (almost)

{% file src="/files/Z9b0Unmo79BPNclDBf6w" %}
The entire thing
{% endfile %}

## Single Plot

```matlab
clc;

clear all;

close all;

x = 0:0.01:2*pi;

plot(x, sin(2*x) + 1, 'r');

xlabel('x');

ylabel('y');

title('Single Plot');

legend('sin(2x) + 1');
```

## Multiple Plot

```matlab
clc;

clear all;

close all;

x = linspace(0,5,100);

plot(x, x.^3, 'k.');

hold on;

plot(x, 3*(x+1), 'b');

xlabel('x');

ylabel('y');

title('Multiple Plot');

legend('y=x^3', 'y=3(x+1)');
```

## Sub Plot

```matlab
clc;

clear all;

close all;

x = linspace(0,5,200);

subplot(1,2,1);

plot(x, cos(3*x), 'b');

xlabel('x');

ylabel('y');

title('Sub Plot');

legend('y=cos(3x)');

subplot(1,2,2);

plot(x, x.^5+2*(x.^2), 'gs');

xlabel('x');

ylabel('y');

title('Sub Plot');

legend('y=x^5+2(x^2)');
```

## Parametric Plot

```matlab
clc;

clear all;

close all;

t = 0:0.01:4*pi;

x = 2*cos(t);

y = 3*sin(t);

plot(x, y, 'r');

xlabel('x');

ylabel('y');

title('Parametric Plot');

legend('Ellipse');
```

## EZ Plot

```matlab
clc;

clear all;

close all;

syms x;

y = tan(2*x) + (9 * x);

ezplot(y, [-2 * pi, 2 * pi]);

xlabel('x');

ylabel('y');

title('EZ Plot');

legend('y=tan(2x) + 9x');
```

## Tangent to a curve

```matlab
clc;

clear all;

close all;

syms x; % Declaring a symbolic variable

y=input('Enter the function f in terms of x:');

x1 = input('Enter x value at which tangent : ');

ezplot(y,[x1-4 x1+4]); % Easy Plotting

hold on;

y_derivative = diff(y,x); % Differentiation in MATLAB

slope = subs(y_derivative,x,x1); % Finding the slope at the given point

y1 = subs(y,x,x1); % Finding the value of the function at the given point

plot(x1,y1,'r:*');

Tgt_line = slope*(x-x1)+y1; % Tangent Line Equation at the given point

h = ezplot(Tgt_line,[x1-4 x1+4]); % Plotting the Tangent Line

set(h,'color','r'); % Coloring the Tangent Line

title('Tangent of a Curve')

% x.^3 + 3*x plotted at x=4
```

## Extremum and inflexion point

```matlab
clc;

clear all;

close all;

syms x real;

f = input('Enter the function f(x):');

fx = diff(f, x);

c = solve(fx);

cmin = min(double(c));

cmax = max(double(c));

ezplot(f, [cmin-1, cmax+1]);

hold on;

fxx= diff(fx, x);

for i = 1:1:size(c)

T1 = subs(fxx, x, c(i));

T3 = subs(f, x, c(i));

if (double(T1) == 0)

sprintf('The point x is %d inflexion point', double (c(i)))

else

if (double(T1) < 0)

sprintf('The maximum point x is %d', double(c(i)))

sprintf('The value of the function is %d', double(T3))

else

sprintf('The minimum point x is %d', double(c(i)))

sprintf('The value of the function is %d', double (T3))

end

end

plot(double(c(i)), double(T3), 'r*', 'markersize', 15);

end
```

## Plot Area of Function

```matlab
clc;

clear all;

close all;

syms x;

f = input('enter the function f(x):');

a = input('enter lower limit of x ');

b = input('enter the upper limit of x');

n = input('number of intervals');

z = int(f,a,b);

value = 0;

dx = (b-a)/n;

for k=1:n

c = a+k*dx;

d = subs(f,x,c);

value = value + d;

end

value = dx*value;

ezplot(f,[a b]);

rsums(f, a, b);
```

## Plot Area between two functions

```matlab
clc;

clear all;

close all;

syms x ;

y1=input('ENTER THE Y1 REGION VALUE');

y2=input('ENTER THE Y2 REGION VALUE');

t=solve(y1-y2); %(Y1-Y2=0)

po=double(t);

poi=sort(po);

n=length(poi);

m1=min(poi);

m2=max(poi);

ez1=ezplot(y1,[m1-1,m2+1]);

hold on;

TA=0;

ez2=ezplot(y2,[m1-1,m2+1]);

if n>2

for i=1:n-1

A=int(y1-y2,poi(i),poi(i+1));

TA= TA+abs(A);

x1 = linspace(poi(i),poi(i+1));

yy1 =subs(y1,x,x1);

yy2 = subs(y2,x,x1);

% Creating a polygon:

xx = [x1,fliplr(x1)];

yy = [yy1,fliplr(yy2)];

fill(xx,yy,'g');

grid on;

end

else

A=int(y1-y2,poi(1),poi(2));

TA=abs(A);

x1 = linspace(poi(1),poi(2));

yy1 =subs(y1,x,x1);

yy2 = subs(y2,x,x1);

xx = [x1,fliplr(x1)];

yy = [yy1,fliplr(yy2)];

fill(xx,yy,'g');

end
```

## Plotting partial derivative of multi-variable function

```matlab
clc;

clear all;

close all;

syms x y

z = input("Enter the 2D function f(x,y)");

x1 = input("Enter the x value at which the derivative has to be evaluated: ");

y1 = input("Enter the y value at which the derivative has to be evaluated: ");

% calculating the slope

z1 = subs(subs(z,x,x1),y,y1);

ezsurf(z, [x1-2 x1+2]);

fdx = diff(z,x);

slopex = subs(subs(fdx, x, x1), y, y1);

%visualisation of plane

[x2, z2] = meshgrid(x1-2:0.25:x1+2, -10:0.5:10);

y2 = y1 * ones(size(x2));

hold on

h1 = surf(x2, y2, z2);

set(h1, 'FaceColor', [0.7,0.7,0.7], 'EdgeColor', 'none');

% the tangent line

t = linspace(-1,1);

x3 = x1 + t;

y3 = y1 * ones(size(t));

z3 = z1 + slopex*t;

line(x3, y3, z3, 'color', 'green', 'linewidth', 2);
```

## Local maxima and minima for two variable function

```matlab
%finding local maxima and minima for two varaibles
clc 
clear all
close all 
%%
syms x y real 
f =input('enter the function')
fx = diff(f,x)
fy = diff(f,y)
[ax ay] = solve(fx,fy)% Solve
fxx = diff(fx,x)
D = fxx*diff(fy,y) - diff(fx,y)^2
%% Collecting critical points
r=1; 
for k=1:1:size(ax) 
if ((imag(ax(k))==0)&&(imag(ay(k))==0)) 
ptx(r)=ax(k); 
pty(r)=ay(k); 
r=r+1; 
end 
end 
%% Visulalizing the funtion
a1=max(double(ax)) 
a2=min(double(ax)) 
b1=max(double(ay)) 
b2=min(double(ay)) 
ezsurf(f,[a2-.5,a1+.5,b2-.5,b1+.5]) 
colormap('summer')
shading interp 
hold on 
%% 
for r1=1:1:(r-1) 
T1=subs(subs(D,x,ptx(r1)),y,pty(r1))
T2=subs(subs(fxx,x,ptx(r1)),y,pty(r1))
if (double(T1) == 0) 
sprintf('The point (x,y) is (%d,%d) and need further investigation', double(ptx(r1)),double(pty(r1))) 
elseif (double(T1) < 0) 
T3=subs(subs(f,x,ptx(r1)),y,pty(r1)) 
sprintf('The point (x,y) is (%d,%d) a saddle point', double(ptx(r1)),double(pty(r1))) 
plot3(double(ptx(r1)),double(pty(r1)),double(T3),'b.','markersize',30); 
else 
if (double(T2) < 0) 
sprintf('The maximum point(x,y) is (%d, %d)', double(ptx(r1)),double(pty(r1))) 
T3=subs(subs(f,x,ptx(r1)),y,pty(r1)) 
sprintf('The value of the function is %d', double(T3)) 
plot3(double(ptx(r1)),double(pty(r1)),double(T3),'r+','markersize',30); 
else 
sprintf('The minimum point(x,y) is (%d, %d)', double(ptx(r1)),double(pty(r1))) 
T3=subs(subs(f,x,ptx(r1)),y,pty(r1)) 
sprintf('The value of the function is %d', double(T3)) 
plot3(double(ptx(r1)),double(pty(r1)),double(T3),'m*','markersize',30); 
end 
end 
end
```

## Maxima & Minima using lagrangian multiplier method

```matlab
clc
clear all
close all
%%
syms x y lam real
f = input('Enter the f(x,y) to be extremized:');
g = input('Enter the constraint function g(x,y):');
F = f - lam*g;
Fd = jacobian(F,[x y lam])
[ax, ay, alam] = solve(Fd, x, y, lam)
ax = double(ax);
ay = double (ay);
T = subs(f, {x,y}, {ax, ay})
T = double(T)
epxl = min(ax);
epxr = max(ax);
epyl = min(ay);
epyu = max(ay);
D = [epxl-0.5 epxr+0.5 epyl-0.5 epyu+0.5]
ezcontourf(f,D)
hold on
h = ezplot(g, D);
set(h, 'color', [1,0.7,0.9])
for i =1:length(T)
	sprintf('The critical point (x,y) is (%1.3f,%1.3f)', ax(i), ay(i))
	sprintf('The critical point (x,y) is (%1.3f,%1.3f)', ax(i), ay(i))
	plot(ax(i),ay(i),'k.','markersize',15)
end
TT=sort(T)
f_min=TT(1)
f_max=TT(end)
```

## Double integral to find volume of sphere of unit radius & visualization

```matlab
clc
clear all 
close all
syms x y z
vol=8*int(int(sqrt(1-x^2-y^2),y,0,sqrt(1-x^2)),x,0,1)
viewSolid(z,0+0*x*y,sqrt(1-x^2-y^2),y,0+0*x,sqrt(1-x^2),x,0,1); 
axis equal; 
grid on;
```

## Volume of solid bounded by plane $z=0$ and paraboloid $z=1-x^2-y^2$

```matlab
clc
clear all
close all
syms r theta 
V = int(int((1-r^2)*r, r, 0, 1), theta, 0, 2*pi)
fsurf(r*cos(theta),r*sin(theta), 1-r^2, [0 1 0 2*pi], 'MeshDensity', 20)
axis equal; axis([-2 2 -2 2 0 1.3])
xticks(-2:2); 
yticks(-2:2); 
zticks(0:1.3)
xlabel('x');
ylabel('y')
```

## Volume of solid under paraboloid $z=x^2+y^2$ and above xy plane, and inside cylinder $x^2 + y^2=2x$

```matlab
clc
clear all
close all
syms r theta z r1
V = int(int((r^3), r, 0,2*cos(theta) ), theta, -pi/2, pi/2)
r = 2*cos(theta), x = r*cos(theta), y = r*sin(theta)
fsurf(x,y,z, [0 pi 0 1], 'MeshDensity', 16)
axis equal; xlabel('x'); ylabel('y'); zlabel('z')
zticks(0:1.5)
hold on
fsurf(r1*cos(theta),r1*sin(theta),r1^2, [0 1 0 2*pi], 'MeshDensity', 20)
```

## Triple Integral 1

```matlab
syms x y z
sol = int(int(int(6*x*z,y,0,x+z),x,0,z),z,0,1)
```

## Triple Integral 2

```matlab
syms x y z
sol = int(int(int(6*x*y,z,0,1+x+y),y,0,sqrt(x)),x,0,1)
viewSolid(z,0+0*x*y,1+x+y,y,0+0*x,sqrt(x),x,0,1)
axis equal;
grid on;
```

## Gradient/Divergence/Curl?

### 2D

```matlab
clc
clear
syms t x y 
f=input('enter the  f vector as i and j order in vector form:');
rbar = input('enter the r vector as i and j order in vector form:');
lim=input('enter the limit of integration:');
vecfi=input('enter the vector field range'); % knowledge of the curve is essential
drbar=diff(rbar,t);
sub = subs(f,[x,y],rbar);
f1=dot(sub,drbar)
int(f1,t,lim(1),lim(2))
P = inline(vectorize(f(1)), 'x', 'y');
Q = inline(vectorize(f(2)), 'x', 'y')
x = linspace(vecfi(1),vecfi(2), 10); y = x; 
[X,Y] = meshgrid(x,y);
U = P(X,Y);
V = Q(X,Y);
quiver(X,Y,U,V)
hold on
fplot(rbar(1),rbar(2),[lim(1),lim(2)])
axis on
xlabel('x')
ylabel('y')
```

### 3D

```matlab
clc
clear
syms t x y z
f=input('enter the  f vector as i j and korder in vector form:');
rbar = input('enter the r vector as i j and k order in vector form:');
lim=input('enter the limit of integration:');
vecfi=input('enter the vector field range'); % knowledge of the curve is essential
drbar=diff(rbar,t);
sub = subs(f,[x,y,z],rbar);
f1=dot(sub,drbar)
int(f1,t,lim(1),lim(2))
P = inline(vectorize(f(1)), 'x', 'y', 'z');
Q = inline(vectorize(f(2)), 'x', 'y', 'z');
R = inline(vectorize(f(3)), 'x', 'y', 'z');
x = linspace(vecfi(1),vecfi(2), 10); y = x; z=x;
[X,Y,Z] = meshgrid(x,y,z);
U = P(X,Y,Z);
V = Q(X,Y,Z);
W = R(X,Y,Z);
quiver3(X,Y,Z,U,V,W)
hold on
t=linspace(lim(1),lim(2),50);
a=subs(rbar(1),t);
b=subs(rbar(2),t);
c=subs(rbar(3),t);
plot3(a,b,c)
axis on
xlabel('x')
ylabel('y')
zlabel('z')
```

## Green's theorem?

```matlab
clc
clear all
syms x y r t
F=input('enter the  F vector as i and j order in vector form:');
integrand=diff(F(2),x)-diff(F(1),y);
polarint=r*subs(integrand,[x,y],[r*cos(t),r*sin(t)]);
sol=int(int(polarint,r,0,1),t,0,2*pi)
P = inline(vectorize(F(1)), 'x', 'y');
Q = inline(vectorize(F(2)), 'x', 'y')
x = linspace(-3.2,3.2, 10); y = x; 
[X,Y] = meshgrid(x,y);
U = P(X,Y);
V = Q(X,Y);
quiver(X,Y,U,V)
hold on
fplot(cos(t),sin(t),[0,2*pi])
axis equal
```
