Scilab Code Solutions

Enhance your skills with real-world coding challenges and comprehensive word problem solutions for effective learning.

Practice Problems

1. Retail Store: Discount Calculator

A retail store offers a discount based on the total purchase amount:

  • 10% if the amount is between ₹500 and ₹1000.

  • 20% if the amount is above ₹1000.

Write a Scilab script to calculate the final amount after discount for a given purchase amount.

// Input purchase amount
amount = input("Enter the purchase amount: ");
// Calculate discount
if amount > 500 & amount <= 1000 then
discount = 0.10* amount;
elseif amount > 1000 then
discount = 0.20 amount;
else discount = 0;
end
// Final amount
final_amount = amount - discount;
disp("Final amount after discount: "), disp(final_amount);

2. Factory Production: Matrix Analysis

A factory produces 3 types of products (A, B, C) in 3 shifts (morning, afternoon, evening).
The production quantities are as follows:
Morning: [50, 60, 70], Afternoon: [40, 50, 60], Evening: [30, 20, 10].

Find:

  • Total production for each product.

  • Total production for each shift.

3. Finance: Compound Interest Calculation

You invest ₹10,000 in a savings account that offers an annual interest rate of 5%.

Write a Scilab script to compute the amount after 10 years, assuming the interest compounds annually.

// Production matrix
production = [50 60 70; 40 50 60; 30 20 10];
// Total production for each product
product_total = sum(production, "r");
disp("Total production for each product: "), disp(product_total);
// Total production for each shift
shift_total = sum(production, "c");
disp("Total production for each shift: "), disp(shift_total);
// Inputs
P = 10000; // Principal amount
r = 5/100; // Annual interest rate
n = 10; // Time in years
// Compound interest formula
A = P * (1 + r)^n;
disp("Amount after 10 years: "), disp(A);

4. Data Science: Exam Analysis

The scores of 5 students in a Math exam are: [78, 85, 62, 90, 88].

Calculate:

  • The average score.

  • The highest and lowest scores.

  • Whether the average score qualifies as "pass" (cutoff: 50).

// Scores
scores = [78, 85, 62, 90, 88];
// Average score
avg_score = mean(scores);
// Highest and lowest scores
max_score = max(scores);
min_score = min(scores);
// Pass/fail decision
if avg_score >= 50 then
result = "Pass";
else
result = "Fail";
end
disp("Average score: "), disp(avg_score);
disp("Highest score: "), disp(max_score);
disp("Lowest score: "), disp(min_score);
disp("Result: "), disp(result);

5. Engineering: Voltage-Current Relationship

In a circuit, the voltage V across a resistor is related to the current I by Ohm's law:
V = IR
If R = 10 Ω, and the current varies as [1, 2, 3, 4, 5] amperes, compute the corresponding voltage values.
// Resistance
R = 10;
// Current values
I = [1, 2, 3, 4, 5];
// Voltage calculation
V = I * R;
disp("Voltage values: "), disp(V);

6. Environment: Rainfall Statistics

A city records the monthly rainfall (in mm) for 6 months as: [120, 85, 95, 110, 140, 75].

  • Find the total rainfall over 6 months.

  • Compute the average monthly rainfall.

  • Determine which month had the highest and lowest rainfall.

// Rainfall data
rainfall = [120, 85, 95, 110, 140, 75];
// Total rainfall
total_rainfall = sum(rainfall);
// Average rainfall
avg_rainfall = mean(rainfall);
// Highest and lowest rainfall
[highest, highest_month] = max(rainfall);
[lowest, lowest_month] = min(rainfall);
disp("Total rainfall: "), disp(total_rainfall);
disp("Average monthly rainfall: "), disp(avg_rainfall);
disp("Month with highest rainfall: "), disp(highest_month);
disp("Month with lowest rainfall: "), disp(lowest_month);

7. Physics: Projectile Motion

A ball is thrown with an initial velocity u = 20 m/s at an angle of 45°.

Write a Scilab script to compute and plot the trajectory of the ball. Use g = 9.81  .

// Parameters
u = 20; // Initial velocity
theta = 45 * %pi / 180; // Convert angle to radians
g = 9.81; // Gravitational acceleration
// Time of flight
T = (2 u sin(theta)) / g;
// Time intervals
t = linspace(0, T, 100);
// Trajectory equations
x = u cos(theta) t;
y = u sin(theta) t - (0.5 g t.^2);
// Plot trajectory
plot(x, y);
xlabel("Horizontal Distance (m)");
ylabel("Vertical Distance (m)");
title("Projectile Motion");