{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "ex_chi2.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "PE1KLXUn95Bk", "colab_type": "text" }, "source": [ "# **תרגיל על התאמות באמצעות פייתון**\n", "**עדיף לצפות בתסריט \"קצר\" בשם**\\\n", "**simple_script_for_chi2_regression**\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "46MwS9Zq-Zhg", "colab_type": "text" }, "source": [ "## **נתון לכם את התוצאות הבאות**\n", "X = -3.00,-2.50,-2.00,-1.50,-1.00,-0.50,0.00,0.50,1.00,1.50,2.00,2.50,3.00,3.50,4.00,4.50,5.00,5.50,6.00,6.50\n", "\n", "\n", "y = -0.22,0.06,0.28,0.23,0.31,0.01,-0.36,-0.17,0.12,0.25,0.14,-0.01,-0.24,-0.32,-1.13,-0.96,-0.34,0.10,0.71,1.01\n", "\n", "\n", "y_err = 0.23,0.16,0.19,0.06,0.27,0.09,0.28,0.21,0.10,0.21,0.06,0.22,0.22,0.22,0.28,0.20,0.05,0.11,0.12,0.13\n", "\n", "## **בצעו התאמה של \"כאי בריבוע\" עבור שתי הפונקציות**:\n", "\n", "**פולינום מסדר 5**\n", "f(x) = a* x^5 + b* x^4 + c* x^3 + d* x^2 + e* x + f \n", "\n", "**פונקציה**\n", "f(x) = a* x* sin(b*x)\n", "\n", "\n", "ותקבעו לפי התוצאות איזה פונקציה מבין שתיהן מתאימה יותר לתוצאות\n" ] }, { "cell_type": "markdown", "metadata": { "id": "inglKyHcA8qJ", "colab_type": "text" }, "source": [ "____________________________________________________________________________________________________________________" ] }, { "cell_type": "code", "metadata": { "id": "wNwjDt__Brl1", "colab_type": "code", "colab": {} }, "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import warnings\n" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "hapCNj3KSS3g", "colab_type": "code", "colab": {} }, "source": [ "" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "mulhgyQdDosf", "colab_type": "code", "colab": {} }, "source": [ "warnings.simplefilter(\"ignore\")" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "7IDUSHEcA5WM", "colab_type": "code", "colab": {} }, "source": [ "!pip install -q matplotlib-venn probfit" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "2puo44PtA66y", "colab_type": "code", "colab": {} }, "source": [ "from probfit import Chi2Regression " ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "ksBF1naEA5e6", "colab_type": "code", "colab": {} }, "source": [ "!pip install -q matplotlib-venn iminuit" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "vd4E0iwCBkVY", "colab_type": "code", "colab": {} }, "source": [ "from iminuit import Minuit" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "4figJlcQHK92", "colab_type": "text" }, "source": [ "### **numpy שימו את הנתונים בתוך מערכים באמצעות**" ] }, { "cell_type": "code", "metadata": { "id": "5Kv1GQ0L-wHH", "colab_type": "code", "colab": {} }, "source": [ "X = np.array([ -3.00,-2.50,-2.00,-1.50,-1.00,-0.50,0.00,0.50,1.00,1.50,2.00,2.50,3.00,3.50,4.00,4.50,5.00,5.50,6.00,6.50])" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "j-wUkqY6Cd7L", "colab_type": "code", "colab": {} }, "source": [ "y = np.array(...)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "z-ZP-Ue9CiIg", "colab_type": "code", "colab": {} }, "source": [ "y_err = ..." ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "FSrRuBkYIVJ3", "colab_type": "text" }, "source": [ "**תגדירות את פונק' הפולינום 5**" ] }, { "cell_type": "code", "metadata": { "id": "fHo2-BZKIk0L", "colab_type": "code", "colab": {} }, "source": [ "pol_5_fun = lambda X,a,b,c,d,e,f: a * X ** 5 + b * X ** 4 +...\n" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "OdHdDqqSJIbH", "colab_type": "text" }, "source": [ "**ביעצו ההתאמה לפי הקוד הבא**" ] }, { "cell_type": "code", "metadata": { "id": "w2gwdnWICm2n", "colab_type": "code", "colab": {} }, "source": [ "#regression\n", "reg = Chi2Regression(pol_5_fun,X,y, error=y_err)\n", "\n", "#optimization\n", "opt = Minuit(reg)\n", "opt.migrad()\n", "\n", "#plot\n", "plt.rc(\"font\", size=16, family=\"Times New Roman\")\n", "fig = plt.figure(figsize=(10, 6))\n", "ax = fig.add_axes([0,0,1,1])\n", "ax.set_xlabel(\"X_data\", fontdict={\"size\": 20, \"weight\":\"bold\"})\n", "ax.set_ylabel(\"y_data\", fontdict={\"size\": 20, \"weight\":\"bold\"})\n", "ax.errorbar(x=X, y=y,yerr=y_err, xerr=X_err, capsize=4, elinewidth=3, fmt='none', ecolor=\"blue\")\n", "ax.scatter(X,y, c='blue', s=30)\n", "reg.show(opt)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "0z7PA63hJUnG", "colab_type": "text" }, "source": [ "**תגדירות את הפונק' עם סינוס**\\\n", "np לא לשכוח שלפני סינוס צריך לכתוב\\\n", "np.sin()" ] }, { "cell_type": "code", "metadata": { "id": "bO9ISpcnKBK3", "colab_type": "code", "colab": {} }, "source": [ "pol_sin = lambda X,a,b:...\n" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "6KeR4gu1KMkI", "colab_type": "text" }, "source": [ "בצעו התאמה לפונקיה\\\n", "נסו לשנות לאתחל את הפרמטרים מאחד כדי לקבל תוצאות יותר טובות\\\n", "opt = Minuit(reg, a=1, b=1)\n" ] }, { "cell_type": "code", "metadata": { "id": "ZdGwH3tfDUSA", "colab_type": "code", "colab": {} }, "source": [ "......" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "kc7MIPVxGvCL", "colab_type": "text" }, "source": [ "### **אזת איזה פונקציה מתאימה יותר?**" ] }, { "cell_type": "code", "metadata": { "id": "H63So-GEG4N6", "colab_type": "code", "colab": {} }, "source": [ "" ], "execution_count": 0, "outputs": [] } ] }