{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set_cordic_iterations (generic function with 1 method)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "include(\"program.jl\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Plots.PlotlyBackend()" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Plots\n", "using Random\n", "using Distributions\n", "\n", "plotly()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Zadanie 10, Franiszek malinka, Kacper Solecki\n", "\n", "# instrukcja:\n", "# Nasz program udostępnia funkcje \n", "\n", "# -> taylor_sin(a, b) - sinus liczby a+bi liczony za pomocą szeregu Taylora\n", "# -> taylor_cos(a, b) - cosinus liczby a+bi liczony za pomocą szeregu Taylora\n", "# -> taylor_sinh(x) - sinus hiperboliczny liczby x liczony za pomocą szeregu Taylora\n", "# -> taylor_cosh(x) - cosinus hiperboliczny liczby x liczony za pomocą szeregu Taylora\n", "# -> cordic_sin(x) - sinus (rzeczywistej) liczby x liczony za pomocą algorytmu Cordic\n", "# -> cordic_cos(x) - cosinus (rzeczywistej) liczby x liczony za pomocą algorytmu Cordic" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9092974268256817" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# porównianie na sin(2), cos(2)\n", "sin(2.0)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0.9092974268256817, -0.0)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "taylor_sin(2.0, 0.0)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9092974280938506" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cordic_sin(2.0)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.4161468365471424" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cos(2)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-0.41614683654714246, -0.0)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "taylor_cos(2.0, 0.0)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.4161468353122473" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cordic_cos(2.0)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-5991.431207677988 - 9240.89014825243im" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# porównianie na sin(10 + 10i)\n", "sin(10 + 10im)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-5991.431207678, -9240.890148252452)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "taylor_sin(10, 10)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "rel_error (generic function with 1 method)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# w ten sposób liczony jest błąd względny zarówno dla liczb rzeczywistych jak i zespolonych\n", "function rel_error(x, y)\n", " if x == 0\n", " return 0\n", " end\n", " return abs((x-y)/x)\n", "end" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# Funkcje użyte w wykresach błędów od liczby iteracji:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "taylor_error_of_iterations (generic function with 1 method)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# błąd przy liczeniu sin(100 + 100i) szeregiem Taylora przy x iteracjach\n", "function taylor_error_of_iterations(x)\n", " set_taylor_iterations(x)\n", " return rel_error(sin(100+100im), taylor_sin(100, 100)[1] + taylor_sin(100, 100)[2]*im)\n", "end" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "cordic_error_of_iterations (generic function with 1 method)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# błąd przy liczeniu sin(100) Cordicem przy x iteracjach\n", "function cordic_error_of_iterations(x)\n", " set_cordic_iterations(x)\n", " return rel_error(sin(100), cordic_sin(100.0))\n", "end" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "taylor_error_of_iterations2 (generic function with 1 method)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# błąd przy liczeniu sin(100) szeregiem Taylora przy x iteracjach\n", "function taylor_error_of_iterations2(x)\n", " set_taylor_iterations(x)\n", " return rel_error(sin(100), taylor_sin(100.0, 0.0)[1])\n", "end" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1:20" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = 1:20" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "# Przykładowe błędy w zależności od liczby iteracji\n", "# obrazują jak szybko zbiega metoda:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "data": [ { "colorbar": { "title": "" }, "legendgroup": "y1", "line": { "color": "rgba(0, 154, 250, 1.000)", "dash": "solid", "shape": "linear", "width": 1 }, "mode": "lines", "name": "y1", "showlegend": true, "type": "scatter", "x": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ], "xaxis": "x", "y": [ 0.9999999999947897, 0.6604336394521374, 0.02055234411557763, 0.00022080130144359062, 1.4713452359772989e-06, 6.728428696183563e-09, 2.238852117625799e-11, 7.408349671662484e-14, 2.066101872553143e-14, 2.066101872553143e-14, 2.066101872553143e-14, 2.066101872553143e-14, 2.066101872553143e-14, 2.066101872553143e-14, 2.066101872553143e-14, 2.066101872553143e-14, 2.066101872553143e-14, 2.066101872553143e-14, 2.066101872553143e-14, 2.066101872553143e-14 ], "yaxis": "y", "zmax": null, "zmin": null } ], "layout": { "annotations": [ { "font": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 20 }, "rotation": 0, "showarrow": false, "text": "Taylor relative error calculating sin(100+100i)", "x": 0.5423611111111111, "xanchor": "center", "xref": "paper", "y": 1, "yanchor": "top", "yref": "paper" } ], "height": 400, "legend": { "bgcolor": "rgba(255, 255, 255, 1.000)", "bordercolor": "rgba(0, 0, 0, 1.000)", "borderwidth": 1, "font": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 11 }, "tracegroupgap": 0, "traceorder": "normal", "x": 1, "xanchor": "auto", "y": 1, "yanchor": "auto" }, "margin": { "b": 20, "l": 0, "r": 0, "t": 20 }, "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "showlegend": true, "width": 600, "xaxis": { "anchor": "y", "domain": [ 0.09128390201224845, 0.9934383202099738 ], "gridcolor": "rgba(0, 0, 0, 0.100)", "gridwidth": 0.5, "linecolor": "rgba(0, 0, 0, 1.000)", "mirror": false, "range": [ 0.43000000000000005, 20.57 ], "showgrid": true, "showline": true, "showticklabels": true, "tickangle": 0, "tickcolor": "rgb(0, 0, 0)", "tickfont": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 11 }, "tickmode": "array", "ticks": "inside", "ticktext": [ "5", "10", "15", "20" ], "tickvals": [ 5, 10, 15, 20 ], "title": "iterations", "titlefont": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 15 }, "type": "-", "visible": true, "zeroline": false, "zerolinecolor": "rgba(0, 0, 0, 1.000)" }, "yaxis": { "anchor": "x", "domain": [ 0.07581474190726165, 0.9415463692038496 ], "gridcolor": "rgba(0, 0, 0, 0.100)", "gridwidth": 0.5, "linecolor": "rgba(0, 0, 0, 1.000)", "mirror": false, "range": [ -0.029999999999822412, 1.0299999999946328 ], "showgrid": true, "showline": true, "showticklabels": true, "tickangle": 0, "tickcolor": "rgb(0, 0, 0)", "tickfont": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 11 }, "tickmode": "array", "ticks": "inside", "ticktext": [ "0.00", "0.25", "0.50", "0.75", "1.00" ], "tickvals": [ 0, 0.25, 0.5, 0.75, 1 ], "title": "relative error", "titlefont": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 15 }, "type": "-", "visible": true, "zeroline": false, "zerolinecolor": "rgba(0, 0, 0, 1.000)" } } }, "text/html": [ "\n", "\n", "
\n", "