PART 03 | الجزء الثالث

Intensive Python Course for Electrical Power Engineering

دورة مكثفة في لغة بايثون وتطبيقاتها في هندسة الطاقة الكهربائية

Programming in Power | البرمجة في الطاقة

Twelve step-by-step training units, MATLAB comparisons, runnable Python examples, and engineering exercises. Designed for fourth-year students working from smartphones through Kaggle.

اثنتا عشرة وحدة تدريبية متتابعة مع مقارنة بـ MATLAB وشرح لكل سطر برمجي وأمثلة هندسية وتمارين قابلة للتنفيذ من الهاتف باستخدام Kaggle.

Learning method: Open a blank Python Notebook on Kaggle. For each of the 12 units, copy or type its code yourself into a new code cell. Predict the result, run that cell, read the line-by-line explanations, then change one value and run it again. Complete the units in order. No ready-made course notebook is required. No fixed lecture duration.

طريقة التعلم: أنشئ دفتر Python فارغا في Kaggle، ثم انسخ أو اكتب بنفسك كود كل وحدة من الوحدات الاثنتي عشرة في خلية جديدة مستقلة. توقع النتيجة قبل التشغيل، ثم شغّل الخلية وافهم شرح كل سطر، وغيّر قيمة وأعد التشغيل. أنجز الوحدات بالترتيب. لا حاجة لتنزيل دفتر جاهز، ولا يوجد زمن ثابت لكل وحدة.

Terms: A library is reusable code. A notebook consists of code and text cells. A cell is an individual block that you run.

المصطلحات: المكتبة مجموعة تعليمات جاهزة قابلة لإعادة الاستخدام. الدفتر يتكون من خلايا كود ونصوص. الخلية كتلة مستقلة يمكن تشغيلها.

Progress: 0 / 12 | الإنجاز: 0 / 12
Progress is saved in this browser when storage is available. | يُحفظ التقدم على هذا المتصفح عند توفر التخزين المحلي.

Practice one unit at a time | طريقة التطبيق وحدة بعد أخرى

  1. Open Kaggle Code, sign in, choose New Notebook, and select Python.افتح Kaggle Code وسجّل الدخول وأنشئ New Notebook بلغة Python.
  2. Go to Unit 01, read the example, and type the code or use Copy code to place it in a new Kaggle code cell.اذهب إلى الوحدة 01 واقرأ المثال، ثم اكتب الكود بنفسك أو استخدم زر نسخ الكود وضعه في خلية برمجية جديدة في Kaggle.
  3. Predict the output, run only that code cell (▶), and compare the result with your expectation.توقع النتيجة ثم شغّل تلك الخلية فقط بالزر ▶ وقارن الناتج مع توقعك.
  4. Explain every line, change a number, rerun, and solve the short exercise before marking the unit complete.فسّر كل سطر، ثم غيّر إحدى القيم وأعد التشغيل وحل التمرين قبل وضع علامة إنجاز الوحدة.
  5. Move to Unit 02 and add a new code cell. Repeat up to Unit 12; save your notebook in Kaggle as your personal practice record.انتقل إلى الوحدة 02 وأنشئ خلية جديدة وكرر الخطوات حتى الوحدة 12، ثم احفظ دفترك الخاص على Kaggle ليكون سجل تدريباتك.

Tip: Typing the code develops familiarity with Python symbols and indentation. On a phone, use Copy code when typing is difficult—but always edit at least one line yourself.

نصيحة: كتابة الكود يدويا تساعد على تعلم رموز Python والمسافات البادئة. عند صعوبة الكتابة على الهاتف يمكن استخدام زر النسخ، لكن يجب تعديل سطر واحد على الأقل بنفسك.

Course Units | الوحدات التعليمية

01

Python syntax and variables

قواعد الكتابة والمتغيرات

A variable is a named place for a value. Python is case-sensitive. The # sign begins a comment, and print displays a result.

المتغير اسم نربطه بقيمة. تميّز Python بين الحروف الكبيرة والصغيرة. تبدأ الملاحظة بالرمز # وتعرض print النتيجة.

MATLAB → Python | المقارنةMATLAB: V = 24; I = 3; P = V * I; disp(P)
Python · Kaggle · Unit 01
# A DC load / حمل مستمر
voltage_v = 24
current_a = 3
power_w = voltage_v * current_a
print('Power =', power_w, 'W')

Line-by-line explanation | شرح كل سطر

  1. L1
    The # line is a comment; Python does not execute it.السطر الذي يبدأ بـ # تعليق لا ينفذه البرنامج.
  2. L2
    Store the number 24 in voltage_v, in volts.نخزن العدد 24 في المتغير voltage_v بوحدة الفولت.
  3. L3
    Store current 3 amperes in current_a.نخزن التيار 3 أمبير في current_a.
  4. L4
    Multiply voltage by current; the result is 72 watts.نضرب الجهد بالتيار فنحصل على 72 واط.
  5. L5
    Display a descriptive label, the calculated number, and W.نعرض نصا توضيحيا والقيمة المحسوبة ووحدة الواط.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Change voltage to 48 V and current to 2 A. Predict the new result before running.

غيّر الجهد إلى 48 فولت والتيار إلى 2 أمبير. توقع النتيجة قبل التشغيل.

02

Data types and conversion

أنواع البيانات والتحويل بينها

int is an integer, float is a decimal number, str is text, and bool is True or False. Numbers read as text must be converted before arithmetic.

يمثل int العدد الصحيح، وfloat العدد العشري، وstr النص، وbool الحالة المنطقية. لا تجري عمليات حسابية على النصوص دون تحويلها.

MATLAB → Python | المقارنةMATLAB: str2double('12.5') ; Python: float('12.5')
Python · Kaggle · Unit 02
voltage_text = '12.5'
voltage = float(voltage_text)
current = 2
active = True
power = voltage * current
print(type(voltage).__name__)
print(active, power)

Line-by-line explanation | شرح كل سطر

  1. L1
    Create a string containing the characters 12.5.ننشىء نصا يتكون من المحارف 12.5.
  2. L2
    Convert the text into a decimal (float) number.نحوّل النص إلى عدد عشري باستخدام float.
  3. L3
    Set the current to 2 A (integer).نجعل قيمة التيار 2 أمبير من النوع الصحيح.
  4. L4
    Record a Boolean status using True.نسجل حالة منطقية بقيمة True.
  5. L5
    Multiply numerical voltage and current.نضرب الجهد العددي بالتيار.
  6. L6
    type(voltage) returns the type object float. The attribute .__name__ extracts that type’s name as the text float; print displays it.تعيد type(voltage) نوع المتغير وهو float. ثم تقرأ الخاصية .__name__ اسم هذا النوع كنص float وتعرضه print.
  7. L7
    Display the state and power of 25 W.نعرض الحالة والاستطاعة الناتجة 25 واط.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Try voltage_text = 'abc'. Read the error and restore a valid number.

جرّب القيمة النصية 'abc' واقرأ رسالة الخطأ ثم أعد رقما صالحا.

03

Operators, powers and energy units

العمليات الرياضية ووحدات الطاقة

Use ** for exponentiation, / for division, and * for multiplication. Electrical energy in kWh equals power in kW multiplied by time in hours.

تستخدم ** للأس، و/ للقسمة، و* للضرب. الطاقة بالكيلوواط الساعي تساوي الاستطاعة بالكيلوواط مضروبة بالزمن بالساعات.

MATLAB → Python | المقارنةMATLAB: E = P_kW * hours; Python: energy_kwh = power_kw * hours
Python · Kaggle · Unit 03
power_w = 1500
hours = 3
power_kw = power_w / 1000
energy_kwh = power_kw * hours
cost_per_kwh = 0.20
cost = energy_kwh * cost_per_kwh
print(energy_kwh, 'kWh')
print(round(cost, 2), 'USD')

Line-by-line explanation | شرح كل سطر

  1. L1
    Set the appliance power to 1500 W.نحدد استطاعة الجهاز بـ 1500 واط.
  2. L2
    Define an operating duration of 3 hours.نحدد زمن التشغيل بثلاث ساعات.
  3. L3
    Divide by 1000 to convert watts into kilowatts.نقسم على 1000 للتحويل من واط إلى كيلوواط.
  4. L4
    Multiply kilowatts by hours to calculate 4.5 kWh.نضرب الكيلوواط بالساعات فنحصل على 4.5 كيلوواط ساعي.
  5. L5
    Define a hypothetical tariff of 0.20 USD/kWh.نحدد تعرفة افتراضية مقدارها 0.20 دولار لكل كيلوواط ساعي.
  6. L6
    Calculate the example bill.نحسب كلفة الطاقة في هذا المثال.
  7. L7
    Print the energy value and its unit.نطبع قيمة الطاقة ووحدتها.
  8. L8
    Round the cost to two decimal places.نقرب الكلفة إلى منزلتين عشريتين.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Replace 1500 W with 5500 W and 3 hours with 0.5 hour.

استبدل 1500 واط بـ 5500 واط، و3 ساعات بنصف ساعة.

04

Conditions: if, elif and else

الشروط واتخاذ القرار

Indentation determines which instructions belong to a condition. The code chooses an engineering message according to the battery state of charge.

تحدد المسافات البادئة التعليمات التابعة للشرط. يختار المثال رسالة هندسية بحسب حالة شحن البطارية.

MATLAB → Python | المقارنةMATLAB: if soc < 20 ... elseif soc < 80 ... else ... end
Python · Kaggle · Unit 04
soc = 35  # percent
if soc < 20:
    message = 'Low battery'
elif soc < 80:
    message = 'Normal range'
else:
    message = 'Nearly full'
print(message)

Line-by-line explanation | شرح كل سطر

  1. L1
    Record state of charge as 35 percent.نسجل حالة الشحن بنسبة 35 بالمئة.
  2. L2
    Ask whether charge is below 20 percent; the colon opens a block.نفحص هل الشحن أقل من 20 بالمئة؛ النقطتان تفتتحان الكتلة.
  3. L3
    Choose the low-battery text only if the condition is true.نختار نص انخفاض الشحن إذا تحقق الشرط فقط.
  4. L4
    Otherwise check whether the value is below 80 percent.وإلا نختبر هل النسبة أقل من 80 بالمئة.
  5. L5
    Store the normal-range message for 35 percent.نحفظ رسالة المجال الطبيعي عند 35 بالمئة.
  6. L6
    Otherwise handle the remaining values.وإلا نعالج القيم الباقية.
  7. L7
    Choose nearly full for values of at least 80 percent.نختار «قريب من الامتلاء» عندما تكون النسبة 80 أو أكثر.
  8. L8
    Print the selected message after the conditional block.نعرض الرسالة المختارة بعد انتهاء الشروط.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Test SOC = 10, 35 and 95. Why does exactly 80 enter the else branch?

اختبر SOC = 10 و35 و95. لماذا تدخل القيمة 80 إلى حالة else؟

05

Loops: for and while

الحلقات التكرارية

A for loop processes a known series of measurements. A while loop repeats only while its condition remains true; always make sure it can stop.

تعالج حلقة for سلسلة قياسات محددة. وتستمر while ما دام شرطها متحققا؛ يجب ضمان إمكانية توقفها.

MATLAB → Python | المقارنةMATLAB: for k = 1:length(loads), ... end; Python indices begin at 0.
Python · Kaggle · Unit 05
loads_kw = [1.0, 1.5, 2.0, 1.5]
energy_kwh = 0
for load in loads_kw:
    energy_kwh += load * 1
print('Energy:', energy_kwh)
count = 0
while count < 2:
    print('Check', count)
    count += 1

Line-by-line explanation | شرح كل سطر

  1. L1
    Define four hourly power readings in kW.نحدد أربع قراءات استطاعة ساعية بالكيلوواط.
  2. L2
    Start the accumulated energy at zero.نبدأ الطاقة المتراكمة من الصفر.
  3. L3
    Process each load value in sequence.نعالج كل قيمة حمل على التوالي.
  4. L4
    Add load times 1 hour to the cumulative energy.نضيف استطاعة الحمل مضروبة بساعة واحدة إلى الطاقة.
  5. L5
    Print the total energy: 6 kWh.نطبع مجموع الطاقة وهو 6 كيلوواط ساعي.
  6. L6
    Initialize the repetition counter at zero.نبدأ عداد التكرار من الصفر.
  7. L7
    Repeat while the counter is less than two.نكرر ما دام العداد أصغر من اثنين.
  8. L8
    Print each check number.نطبع رقم الفحص في كل دورة.
  9. L9
    Increase the counter to guarantee termination.نزيد العداد لضمان توقف الحلقة.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Add a fifth 3-kW hour and calculate the new total.

أضف ساعة خامسة باستطاعة 3 كيلوواط واحسب الطاقة الجديدة.

06

Lists, dictionaries and indexing

القوائم والقواميس والفهرسة

A list stores ordered measurements. A dictionary connects meaningful names to values. Python begins counting list positions at index 0.

تحفظ القائمة قيما مرتبة، أما القاموس فيربط أسماء واضحة بالقيم. تبدأ فهرسة قوائم Python من الصفر.

MATLAB → Python | المقارنةMATLAB: load(1) = first value; Python: loads[0] = first value.
Python · Kaggle · Unit 06
loads_kw = [1.2, 2.5, 1.8]
plant = {'name': 'PV1', 'rating_kw': 5}
first_load = loads_kw[0]
last_load = loads_kw[-1]
rating = plant['rating_kw']
print(first_load, last_load)
print(plant['name'], rating)

Line-by-line explanation | شرح كل سطر

  1. L1
    Create a list with three load measurements.ننشىء قائمة تتضمن ثلاث قراءات للحمل.
  2. L2
    Create a dictionary with name and rated power.ننشىء قاموسا يحتوي اسم النظام واستطاعته الاسمية.
  3. L3
    Select the first list item using index 0.نختار أول عنصر في القائمة بالفهرس 0.
  4. L4
    Select the final list item using -1.نختار العنصر الأخير بالفهرس -1.
  5. L5
    Look up the rating by its dictionary key.نقرأ الاستطاعة الاسمية بواسطة مفتاح القاموس.
  6. L6
    Print the first and last loads.نطبع قيمة الحمل الأولى والأخيرة.
  7. L7
    Print the system name and rating.نطبع اسم النظام واستطاعته الاسمية.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Add an inverter efficiency key with value 0.95 and display it.

أضف مفتاحا لكفاءة الإنفرتر بقيمة 0.95 واعرضه.

07

Functions and input validation

الدوال والتحقق من المدخلات

A function packages a calculation into a reusable unit. def defines it, parameters receive inputs, and return sends back the result.

تجمع الدالة عملية حسابية يمكن استخدامها مرات متعددة. تعرّف def الدالة، وتستقبل المعاملات المدخلات، ثم تعيد return النتيجة.

MATLAB → Python | المقارنةMATLAB: function P = dc_power(V,I); P = V*I; end
Python · Kaggle · Unit 07
def dc_power(voltage_v, current_a):
    if voltage_v < 0 or current_a < 0:
        raise ValueError('Use positive values')
    return voltage_v * current_a
p1 = dc_power(24, 3)
p2 = dc_power(48, 2)
print(p1, p2)

Line-by-line explanation | شرح كل سطر

  1. L1
    Define a function with voltage and current inputs.نعرّف دالة تستقبل الجهد والتيار.
  2. L2
    Check whether any input is negative.نتحقق هل أي مدخل سالب.
  3. L3
    Raise a clear error for unsupported inputs.نظهر رسالة خطأ واضحة للمدخلات غير المدعومة.
  4. L4
    Return the product of voltage and current.نعيد ناتج ضرب الجهد بالتيار.
  5. L5
    Call the function for 24 V and 3 A.نستدعي الدالة لجهد 24 فولت وتيار 3 أمبير.
  6. L6
    Call it again for 48 V and 2 A.نستدعيها مرة أخرى لجهد 48 فولت وتيار 2 أمبير.
  7. L7
    Print both calculated power values.نطبع قيمتي الاستطاعة المحسوبتين.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Write a new function energy_kwh(power_kw, hours). Validate nonnegative inputs.

اكتب دالة جديدة energy_kwh(power_kw, hours) وتحقق من عدم سلبية المدخلات.

08

NumPy for numerical engineering

الحسابات العددية باستخدام NumPy

A library is reusable code written by others. NumPy is a numerical library; arrays allow one operation to be applied to many samples efficiently.

المكتبة شيفرة برمجية جاهزة كتبها آخرون لإعادة استخدامها. NumPy مكتبة للحسابات العددية تسمح بإجراء العملية نفسها على عينات عديدة.

MATLAB → Python | المقارنةMATLAB: power = voltage .* current; Python NumPy: power = voltage * current
Python · Kaggle · Unit 08
import numpy as np
voltage = np.array([220, 225, 230])
current = np.array([2, 3, 4])
power = voltage * current
average = np.mean(power)
safe_voltage = np.clip(voltage, 222, 230)
print(power.tolist())
print(round(average, 2))
print(safe_voltage.tolist())

Line-by-line explanation | شرح كل سطر

  1. L1
    Import NumPy and give it the short name np.نستورد مكتبة NumPy ونسميها اختصارا np.
  2. L2
    Create an array of three voltage values.ننشىء مصفوفة تضم ثلاث قيم للجهد.
  3. L3
    Create a matching array of current values.ننشىء مصفوفة مقابلة لقيم التيار.
  4. L4
    Multiply corresponding values element by element.نضرب القيم المتقابلة عنصرا بعنصر.
  5. L5
    Calculate the average power.نحسب متوسط الاستطاعة.
  6. L6
    Limit the voltage values to the range 222–230 V for illustration.نحصر قيم الجهد بين 222 و230 فولت للتوضيح.
  7. L7
    Convert the result to a list and print it.نحوّل الناتج إلى قائمة ونعرضه.
  8. L8
    Round and print the average.نقرب المتوسط ونعرضه.
  9. L9
    Show the illustrative clipped values.نعرض القيم بعد التحديد التوضيحي.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Calculate maximum power with np.max(power); compare with the hand calculation.

احسب أكبر استطاعة باستخدام np.max(power) وقارنها بالحساب اليدوي.

09

Pandas for measurement tables

جداول القياسات باستخدام Pandas

Pandas handles tabular data like spreadsheet rows and columns. Here we create a small CSV text inside the notebook, so no download or account permission is needed.

تعالج Pandas الجداول ذات الصفوف والأعمدة. نصنع هنا جدول CSV ضمن الدفتر مباشرة دون الحاجة لتنزيل بيانات.

MATLAB → Python | المقارنةMATLAB: T = readtable(...); Python: df = pd.read_csv(...)
Python · Kaggle · Unit 09
import pandas as pd
from io import StringIO
csv = 'hour,load_kw\n0,1.2\n1,1.8\n2,2.5'
df = pd.read_csv(StringIO(csv))
df['energy_kwh'] = df['load_kw'] * 1
total = df['energy_kwh'].sum()
print(df.to_string(index=False))
print('Total:', total, 'kWh')

Line-by-line explanation | شرح كل سطر

  1. L1
    Import the tabular-data library Pandas.نستورد مكتبة Pandas الخاصة بالبيانات الجدولية.
  2. L2
    Import StringIO to treat text like a file.نستورد StringIO للتعامل مع النص كأنه ملف.
  3. L3
    Define CSV text with column names and three rows; \n marks a new line.نحدد نص CSV بأسماء الأعمدة وثلاثة صفوف؛ الرمز \n يعني سطرا جديدا.
  4. L4
    Read the text into a DataFrame table.نقرأ النص في جدول DataFrame.
  5. L5
    Create an energy column assuming 1-hour intervals.ننشىء عمود طاقة بافتراض أن كل فترة ساعة واحدة.
  6. L6
    Sum the new energy column.نجمع قيم عمود الطاقة.
  7. L7
    Display the table without row indices.نعرض الجدول دون أرقام الصفوف.
  8. L8
    Print total energy, 5.5 kWh.نطبع مجموع الطاقة وهو 5.5 كيلوواط ساعي.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Add a fourth row for hour 3 with 2.0 kW. Recalculate the sum.

أضف صفا رابعا للساعة 3 وباستطاعة 2.0 كيلوواط وأعد حساب المجموع.

10

Matplotlib and engineering plots

الرسم البياني باستخدام Matplotlib

A graph needs labeled axes, a title, units and readable markers. Matplotlib produces figures inside Kaggle notebooks without any local installation.

يحتاج الرسم إلى محورين مسمّيين وعنوان ووحدات وعلامات واضحة. تنشئ Matplotlib الرسوم ضمن دفاتر Kaggle دون تثبيت محلي.

MATLAB → Python | المقارنةMATLAB: plot(hours, load_kw); xlabel('Hour'); ylabel('kW')
Python · Kaggle · Unit 10
import matplotlib.pyplot as plt
hours = [0, 1, 2, 3]
load_kw = [1.2, 1.8, 2.5, 1.6]
plt.plot(hours, load_kw, marker='o')
plt.xlabel('Hour')
plt.ylabel('Load (kW)')
plt.title('Hourly electrical load')
plt.grid(True)
plt.show()

Line-by-line explanation | شرح كل سطر

  1. L1
    Import the plotting part of Matplotlib.نستورد قسم الرسم من مكتبة Matplotlib.
  2. L2
    Define the time values on the horizontal axis.نحدد قيم الزمن للمحور الأفقي.
  3. L3
    Define the load samples on the vertical axis.نحدد عينات الحمل للمحور العمودي.
  4. L4
    Draw the load curve and mark every sample with a circle.نرسم منحني الحمل ونميز كل عينة بدائرة.
  5. L5
    Label the horizontal axis Hour.نسمي المحور الأفقي Hour.
  6. L6
    Label the vertical axis and include the kW unit.نسمي المحور العمودي مع وحدة الكيلوواط.
  7. L7
    Add a descriptive chart title.نضيف عنوانا واضحا للرسم.
  8. L8
    Show a grid to make values easier to read.نظهر شبكة لتسهيل قراءة القيم.
  9. L9
    Display the completed figure.نعرض الشكل النهائي.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Change the third sample to 3.5 kW. Identify the peak hour.

غيّر القراءة الثالثة إلى 3.5 كيلوواط وحدد ساعة الذروة.

11

Complex power in AC circuits

الاستطاعة العقدية في دارات التيار المتناوب

Python uses j for the imaginary unit. For RMS phasors and passive sign convention, complex power is S = V × conjugate(I), with real part P and imaginary part Q.

تستخدم Python الحرف j للوحدة التخيلية. عند استعمال الفازورات بالقيم الفعالة واتفاقية الحمل تكون الاستطاعة العقدية S = V × مرافق(I)، وجزؤها الحقيقي P والتخيلي Q.

MATLAB → Python | المقارنةMATLAB: S = V * conj(I); Python: S = V * I.conjugate()
Python · Kaggle · Unit 11
import cmath
voltage = 230 + 0j
current = 8 * cmath.exp(-1j * cmath.pi / 6)
s = voltage * current.conjugate()
p_w = s.real
q_var = s.imag
print(round(p_w), 'W')
print(round(q_var), 'var')

Line-by-line explanation | شرح كل سطر

  1. L1
    Import complex-math functions.نستورد دوال العمليات على الأعداد العقدية.
  2. L2
    Represent a 230-V RMS reference phasor.نمثل فازور جهد مرجعي بالقيمة الفعالة 230 فولت.
  3. L3
    Represent 8-A RMS current lagging voltage by 30 degrees.نمثل فازور تيار فعال 8 أمبير متأخرا عن الجهد بـ 30 درجة.
  4. L4
    Multiply voltage by the current's complex conjugate.نضرب الجهد بالمرافق العقدي للتيار.
  5. L5
    Extract active power from the real part of S.نستخرج الاستطاعة الفعالة من الجزء الحقيقي لـ S.
  6. L6
    Extract reactive power from the imaginary part of S.نستخرج الاستطاعة الردية من الجزء التخيلي لـ S.
  7. L7
    Print approximate active power in watts.نطبع الاستطاعة الفعالة التقريبية بالواط.
  8. L8
    Print approximate reactive power in var.نطبع الاستطاعة الردية التقريبية بالفار.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Change the current phase to +30 degrees. What happens to reactive power?

غيّر زاوية التيار إلى +30 درجة. ماذا يحدث لإشارة الاستطاعة الردية؟

12

Mini-project: PV, load and battery

المشروع المصغر: الطاقة الشمسية والحمل والبطارية

A first energy-balance simulation with one-hour steps, a 90% one-way charge/discharge efficiency, no export, and no power limit. It teaches algorithms, not a complete real-world design.

محاكاة أولية لميزان الطاقة بخطوة زمنية قدرها ساعة، وكفاءة شحن وتفريغ أحادية الاتجاه 90%، دون تصدير أو حد للاستطاعة. الهدف تعلم الخوارزمية وليس اعتماد تصميم هندسي جاهز.

MATLAB → Python | المقارنةMATLAB: use a for loop, if/else, and vector indexing 1...N. Python indexes 0...N-1.
Python · Kaggle · Unit 12
load_kw = [2, 3, 2, 1]
pv_kw = [0, 4, 3, 0]
soc = 2.0  # stored kWh
capacity = 4.0
eta = 0.90
for hour in range(len(load_kw)):
    net_kw = pv_kw[hour] - load_kw[hour]
    if net_kw >= 0:
        charge_kw = min(net_kw, (capacity - soc) / eta)
        soc += charge_kw * eta
        grid_import = 0.0
    else:
        discharge_kw = min(-net_kw, soc * eta)
        soc -= discharge_kw / eta
        grid_import = -net_kw - discharge_kw
    print(hour, round(soc, 2), round(grid_import, 2))

Line-by-line explanation | شرح كل سطر

  1. L1
    Enter four hourly loads in kW.ندخل أربعة أحمال ساعية بالكيلوواط.
  2. L2
    Enter PV generation during the same four hours.ندخل استطاعة الألواح الشمسية خلال الساعات نفسها.
  3. L3
    Set the initial stored battery energy to 2 kWh.نحدد الطاقة المخزنة أوليا في البطارية بـ 2 كيلوواط ساعي.
  4. L4
    Set the battery's idealized energy capacity to 4 kWh.نحدد السعة الطاقية للبطارية بـ 4 كيلوواط ساعي.
  5. L5
    Specify a 90% one-way efficiency.نحدد كفاءة أحادية الاتجاه مقدارها 90 بالمئة.
  6. L6
    Loop over all four one-hour periods.نكرر الحساب على الفترات الأربع، طول كل منها ساعة.
  7. L7
    Calculate PV generation minus load.نحسب استطاعة التوليد الشمسي ناقص الحمل.
  8. L8
    If the result is nonnegative, PV covers the load.إذا كانت النتيجة غير سالبة فالتوليد الشمسي يغطي الحمل.
  9. L9
    Limit charging to available surplus and remaining storage capacity.نحد الشحن بالفائض المتاح والمساحة المتبقية في البطارية.
  10. L10
    Update stored energy with charging losses.نحدّث الطاقة المخزنة مع خسائر الشحن.
  11. L11
    No grid import is required while generation meets demand.لا حاجة للسحب من الشبكة ما دام التوليد يغطي الطلب.
  12. L12
    Otherwise the load exceeds PV generation.وإلا يكون الحمل أكبر من التوليد الشمسي.
  13. L13
    Limit battery discharge by deficit and available energy.نحد تفريغ البطارية بالعجز والطاقة المتاحة.
  14. L14
    Subtract battery energy including discharge losses.نطرح طاقة البطارية مع أخذ خسائر التفريغ بالحسبان.
  15. L15
    Import from grid only the deficit not supplied by the battery.نسحب من الشبكة فقط العجز الذي لم تغطه البطارية.
  16. L16
    Print hour, remaining stored energy and grid import.نطبع الساعة والطاقة المتبقية في البطارية والاستطاعة المسحوبة من الشبكة.
Predict → Run → Explain | توقع ← شغّل ← فسّر

Reduce initial SOC to 0.5 kWh and compare grid energy. Explain why this model still cannot size a real battery.

خفّض طاقة البطارية الأولية إلى 0.5 كيلوواط ساعي وقارن السحب من الشبكة. فسّر لماذا لا يكفي هذا النموذج لتصميم بطارية حقيقية.

Quick reference | مرجع سريع للمصطلحات

Python termالمعنىMATLAB bridge
variableمتغيرassignment / إسناد
list, arrayقائمة، مصفوفةvector / شعاع
index 0الفهرسة من الصفرMATLAB starts at 1
def, returnتعريف الدالة وإرجاع الناتجfunction
importاستيراد مكتبةtoolboxes
DataFrameجدول بياناتtable
j, conjugate()الجزء التخيلي والمرافق1j, conj()

Engineering note: All examples use explicitly stated assumptions; the last project is an educational algorithm rather than a validated sizing design. ML and AI models are intentionally reserved for the subsequent AI courses.

ملاحظة هندسية: تعتمد الأمثلة على افتراضات محددة. المشروع الأخير خوارزمية تعليمية لا تغني عن دراسة التصميم والتحقق. تُترك نماذج تعلم الآلة والذكاء الاصطناعي لمقررات الذكاء الاصطناعي اللاحقة.

Knowledge Check: 10 Questions | اختبار تفاعلي: 10 أسئلة

Select one answer for each question. Passing target 8/10. | اختر إجابة واحدة لكل سؤال. الهدف 8 من 10.

1. Which index selects the first Python list element?أي فهرس يختار أول عنصر من قائمة Python؟
2. What does float('12.5') return?ماذا تعيد float('12.5')؟
3. What is the energy of a 2-kW load running for 3 hours?ما طاقة حمل استطاعته 2 كيلوواط يعمل 3 ساعات؟
4. What is the role of indentation after if?ما دور المسافة البادئة بعد if؟
5. Which command creates a reusable function?أي تعليمة تنشئ دالة قابلة لإعادة الاستخدام؟
6. Which NumPy expression multiplies corresponding array elements?أي عبارة في NumPy تضرب عناصر المصفوفتين المتقابلة؟
7. Which Pandas structure stores a table?أي بنية في Pandas تحفظ جدولا؟
8. Why add a y-axis label to an electrical plot?لماذا نضيف تسمية إلى المحور الرأسي لمنحنى كهربائي؟
9. For RMS phasors and load convention, complex power is:للفازورات الفعالة ووفق اتفاقية الحمل، الاستطاعة العقدية هي:
10. Why is the battery mini-project not a final design?لماذا لا يمثل مشروع البطارية تصميما نهائيا؟