-
x = poly(0, 'x');
f = x^3 + 2*x^2 + 3*x;
df = derivat(f);
disp("Derivative of f(x):");
disp(df);
g = x^2 + 3*x + 2;
integral_g = polyint(g);
disp("Integral of g(x):");
disp(integral_g);
p = [1 -5 6];
r = roots(p);
disp("Roots of equation:");
disp(r);
-
t = 0:0.001:1;
f = 5;
y = sin(2 * %pi * f * t);
playsnd(y, 1000);
disp("Sound is being played...");
img = imread("D:\scilab\image.jpg");
imshow(img);
title("Original Image");
gray_img = rgb2gray(img);
figure();
imshow(gray_img);
title("Grayscale Image");
-
x = 0:0.1:10;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, 'r');
plot(x, y2, 'b');
legend("sin(x)", "cos(x)");
title("Advanced 2D Plotting");
xlabel("X-axis");
ylabel("Y-axis");
xgrid();
[X, Y] = meshgrid(-5:1:5, -5:1:5);
Z = X.^2 + Y.^2;
scf(1);
plot3d(X, Y, Z);
title("3D Surface Plot: Z = X^2 + Y^2");
xlabel("X-axis");
ylabel("Y-axis");
zlabel("Z-axis");
-
student.name = "Alice";
student.age = 21;
student.marks = [85 90 95];
disp("Student Details:");
disp("Name: " + student.name);
disp("Age: " + string(student.age));
disp("Marks:");
disp(student.marks);
x = 0:0.1:10;
y = sin(x);
plot(x, y);
title("Sine Wave");
xlabel("X-axis");
ylabel("Y-axis");
xgrid();
plot(x, y, 'r-');
-
function [sum, product]=operations(a, b)
sum = a + b;
product = a * b;
endfunction
function fact=factorial(n)
if n == 0 then
fact = 1;
else
fact = n * factorial(n - 1);
end
endfunction
// save as main.sce
exec('D:\scilab\operations.sci',-1);
a = input("Enter first number: ");
b = input("Enter second number: ");
n = input("Enter number for factorial: ");
[s, p] = operations(a, b);
f = factorial(n);
disp("Results of Operations:");
disp("Sum = " + string(s));
disp("Product = " + string(p));
disp("Factorial:");
disp("Factorial of " + string(n) + " = " + string(f));
-
data = [10 20 30 40 50];
fd = mopen('data.txt', 'wt');
for i=1:length(data)
mfprintf(fd,"%d\n",data(i));
end
mclose(fd);
fd = mopen('data.txt', 'rt');
str_data = mgetl(fd);
mclose(fd);
read_data=evstr(str_data);
disp("Data written to file:");
disp(data);
disp("Data read from file:");
for i=1:length(read_data)
disp(read_data(i));
end
-
cell_arr = list(10, "Scilab", [1 2 3]);
disp("Cell Array Elements:");
disp(cell_arr(1));
disp(cell_arr(2));
disp(cell_arr(3));
student.name = "John";
student.age = 20;
student.marks = [85 90 88];
disp("Structure Elements:");
disp("Name: " + student.name);
disp("Age: " + string(student.age));
disp("Marks:");
disp(student.marks);
-
str1 = "Scilab";
str2 = "Programming";
len = length(str1);
upper_str = convstr(str1, "u");
lower_str = convstr(str2, "l");
concat_str = str1 + " " + str2;
disp("String Operations:");
disp("Length of str1 = " + string(len));
disp("Uppercase = " + upper_str);
disp("Lowercase = " + lower_str);
disp("Concatenation = " + concat_str);
num = input("Enter a number: ");
num_str = string(num);
str_num = "123";
converted_num = evstr(str_num);
disp("Conversions:");
disp("Number to String = " + num_str);
disp("String to Number = " + string(converted_num));
-
1)sum_functions.sci
function result=add_numbers(a, b)
result=a+b;
endfunction
2)main_script.sci
exec('sum_functions.sci',-1);
a=input("Enter first number:");
b=input("Enter second number:");
sum=add_numbers(a,b);
disp("sum of two numbes:");
disp(sum);
-
total=0;
n=input("Enter a number:");
for i=1:n
total=total+i;
end
disp("Sum using FOR loop");
disp(total);
fact=1;
i=1;
while i<=n
fact=fact*i;
i=i+1;
end
disp("Factorial using WHILE loop");
disp(fact);
v=1:n;
vector_sum=v*ones(n,1);
disp("Sum using vectorization:");
disp(vector_sum);
-
num=input("Enter a number:");
choice=input("Enter a number between 1 and 3:");
if num>0 then
disp("The number is positive");
elseif num==0
disp("The number is zero");
else
disp("The number is negative");
end-5
switch choice
case 1 then
disp("You selected one");
case 2 then
disp("You selected two");
case 3 then
disp("You selected three");
else
disp("Invalid choice");
end
-
a=input("Enter first number:");
b=input("Enter second number:");
sum=a+b;
disp("Entered values:");
disp("First Number:"+string(a));
disp("Second Number:"+string(b));
disp("Result:")
disp("Sum="+string(sum));
-
v1=[1,2,3];
v2=[4,5,6];
v_add=v1+v2;
v_sub=v1-v2;
v_mul=v1.*v2;
disp("Vector Operations:");
disp("Addition:");
disp(v_add);
disp("Subtraction:");
disp(v_sub);
disp("Element wise multiplication:");
disp(v_mul);
A=[1 2;3 4];
B=[5 6;7 8];
m_add=A+B;
m_sub=A-B;
m_mul=A*B;
m_transpose=A';
disp("Matrix Operations:");
disp("Addition:");
disp(m_add);
disp("Subtraction:");
disp(m_sub);
disp("Multiplication:");
disp(m_mul);
disp("Transpose of A:");
disp(m_transpose);
-
a=10;
b=5;
add=a+b;
sub=a-b;
mul=a*b;
div=a/b; modulus=modulo(a,b);
power=a^b;
disp("Mathematical Operations:");
disp("Addition:"+string(add));
disp("Subtraction:"+string(sub));
disp("Multiplication:"+string(mul));
disp("Division:"+string(div));
disp("Modulus:"+string(modulus));
disp("Power:"+string(power));
disp("Relational Operations");
disp("a>b="+string(a>b));
disp("a<b="+string(a<b));
disp("a==b="+string(a==b));
disp("a~=b="+string(a~=b));
disp("a>=b="+string(a>=b));
disp("a<=b="+string(a<=b));
A robust way to load JSON configuration data with built-in error handling.
import json
import os
def load_json_file(file_path):
if not os.path.exists(file_path):
print(f"Error: {file_path} does not exist.")
return None
try:
with open(file_path, 'r', encoding='utf-8') as file:
return json.load(file)
except json.JSONDecodeError as e:
print(f"Error parsing JSON: {e}")
return None
Limits the rate at which a function can fire. Essential for search bars and window resize events.
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}