graphics_toolkit qt; # when using Octave-GUI include this line. If working with octave-cli on jupyter exclude this
x = [-2, 1.5, 0, 1/2*pi, pi]; disp(["x = : ", num2str(x)]);
xa = abs(x); disp(["x = : ", num2str(x), "\nabs(x) = :", num2str(xa)]);
b = cos(x); disp(b);
c = sin(x); disp(c);
d = tan(x); disp(d);
bi = acos(b); disp(bi);
ci = asin(c); disp(ci);
di = atan(d); disp(di);
disp(["x = : ", num2str(x)]);
e = atan2(-1, 1); disp(e);
f = atan2(1, 1); disp(f);
disp(pi/4)
y = [-2, -1, 1, 2];
ys = sinh((y)); disp(ys);
ysp1 = exp(y); ysp2 = exp(-y);
ys2 = 1/2*(ysp1 - ysp2); disp(ys); disp(ys2);
c = 2 + 3j;
disp(abs(c));
disp(sqrt(2^2 + 3^2));
disp(real(c));
disp(imag(c));
disp(conj(c));
d = [2+3j, 1-1j, 4+pi*1j]; disp(d);
dc = conj(d); disp(dc)
x = linspace(-2, 2, 100);
y = cos(2*pi*x);
z = sin(2*pi*x);
# We specify the figure window in which plot will be present
figure(1);
# We plot cos(X) as a dotted line. We also specify the size of the "." marker as 10
plot(x, y,'.',"markersize",10);
hold on;
# We plot tan(x) and sin(x) as continuous lines with a specified linewidth of 5
plot(x, z,"linewidth", 5);
plot(x, tan(x),"linewidth", 5);
# We specify the labels for x and y axis with a specified fontsize and font name
xlabel("x", "fontsize",20,"fontname", "TimesNewRoman");
ylabel("y","fontsize",20,"fontname", "TimesNewRoman");
# We specify the plot title with a specified fontsize and font name
title("Some functions plotted", "fontsize",20,"fontname", "TimesNewRoman");
# We specify the extent of x and y axis
ylim([-1,1]);
xlim([0, 2]);
# We specify the legend values and also change the position of the legend
h = legend ("cos(x)", "sin(x)", "tan(x)");
legend (h, "location", "southwest");
# We specify the font size and font name for the legend and the axes
set (h, "fontsize", 14,"fontname", "TimesNewRoman");
set(gca, "fontsize",20,"fontname", "TimesNewRoman")
x = linspace(0, 10, 100);
y = besselj(0,x);
figure(1);
plot(x, y, "linewidth", 5);
xlabel("x","fontsize", 20,"fontname", "TimesNewRoman"); ylabel("J_0(x)","fontsize", 20,"fontname", "TimesNewRoman")
set(gca,'FontSize',20,"fontname", "TimesNewRoman")
# Enable the grid
grid on;
for i = 0:4
y = besselj(i ,x);
figure(2);
plot(x, y, sprintf("-;J_{%d};",i), "linewidth",5); # specify the legend entry inline
hold on # hold on the plot window for plotting all the functions in same window
end
# plot the y=0 line i.e. the x axis
plot(x, 0.*x, '--r', "linewidth",2);
hold off;
ax = gca();
set(gca,'FontSize',20,"fontname", "TimesNewRoman");
# Enable the legend and modify its position font size and font name
h = legend();
legend (h, "location", "southwest");
set (h, "fontsize", 10,"fontname", "TimesNewRoman");
legend boxoff
# specify the x and y labels
ylabel("Bessel function","fontsize", 20,"fontname", "TimesNewRoman");
xlabel("x","fontsize", 20,"fontname", "TimesNewRoman");
grid on;
function [area,circ] = circ_properties(r)
# local variables - scope is limited to the definition of the function
area = pi*r.^2;
circ = 2*pi*r;
end
We calculate the area and circumference and store them in variables and then return those values
We can now call the function for any radius
[x ,y] = circ_properties(1)
printf("For a circle with radius: %f\n", 1)
printf("The area is: %f\n", x);
printf("Circumferance: %f\n", y);
function [y1, y2] = fx(x, c)
y1 = x.^2 + c*x + 2*c;
y2= exp(c);
end
x = linspace(-3, 3);
for i=-2:3
[y, e] = fx(x, i);
plot(x, y, sprintf("-;c = %d;", i), "linewidth",5);
hold on;
end
disp(e);
disp(exp(2));
set(gca,'FontSize',20, "fontname", "TimesNewRoman");
h = legend();
legend (h, "location", "northwest");
set (h, "fontsize", 10,"fontname", "TimesNewRoman");
disp(x);
disp(y)
disp(size(x)); disp(size(y))
fid = fopen('Datafile.txt', 'w+');
fprintf(fid,"%f \t %f\n", x, y);
fclose(fid);
d = load('Datafile.txt');
disp(size(d))
e = [x; y]; disp(e);
fid = fopen('data_human.txt', 'w+');
fprintf(fid,"%1.4f %1.4f \n", e);
fclose(fid);