Wednesday, July 29, 2020

Matlab - Move sinus function toward x axis direction

% The Script plot a Sinus Graph Functin and move it to the x axis direction.
x= 0:0.1:2*pi
y = sin(x);

for i1=0:630
    y = sin(x- i1/100);
    plot(x,y);
    drawnow;
    F(i1+1) = getframe;
end

% One can use the movie function to Play recorded movie frames
% To play the movie two times, use movie(F,2).

Friday, July 24, 2020

Matlab - Read data from Excel file and plot the data

This script use the matlab function readtable and table2array to read data from Excel file and plot the data.

Download the script read_xls.m


Data in Excel file:

Data in Excel file
Data in Excel file


The script:

% This script read data from Excel file
% and put the data in array and plot the data

% Read the data from Excel file Book1.xlsx
% and put the data in table variable t.
disp( "The data read from Book1.xlsx file")
t = readtable("C:\Users\etmos\Desktop\Book1.xlsx")

% Put the data from table variable  t  to variable x, y.
% "x" and "y" are the titles of the data un xlsx file.
x = table2array(t(:, "x"));
y = table2array(t(:, "y"));

% plot thr data.
plot(x,y)

Script Output

>> read_xls
The data read from Book1.xlsx file

t =

  20×2 table

     y     x
    ___    __

      1     1
      4     2
      9     3
     16     4
     25     5
     36     6
     49     7
     64     8
     81     9
    100    10
    121    11
    144    12
    169    13
    196    14
    225    15
    256    16
    289    17
    324    18
    361    19
    400    20


Data Graph



Tuesday, July 7, 2020

Octave - Symbolic calculation - Plot function and its derivetive

This symbilic script get input a function f(x), and the order n of the derivetive requsted.
The script plot the function f(x) and the order n derivetive.

Download the file plot_derive_sym_order_n.m

% This script plot derivetive fd(x) of the function f(x).
% The script use the function_handle function to create numeric
% function ffd from dff(x).

pkg load symbolic
setenv PYTHON C:\ProgramData\Anaconda3\pythonw.exe

disp('Order of derivetive:')
n = 2
% Declare symbolic variable x
syms x;

disp('The function f(x):')
% Input the function f(x) here:
f = sin(x) + cos(x)+ x^2*sin(x)

disp('The n order derivetive fd(x) of the function f(x):')
% The derivative of the function f:
% fd(x) = f'(x)
fd(1) = diff(f,x);
i1 = 2;
while i1<=n
      fd(i1) = diff(fd(i1-1), x);
      i1++;
endwhile
fd(n)
% Convert symbolic function fd(x) to numeric function dff(x):
dff = function_handle(fd(n));

x=-2*pi:0.1:2*pi;
y= dff(x);
plot(x,y)
grid
xlabel('x')
ylabel('fd order n(x)')

Output






Monday, July 6, 2020

Symbolic derive of function and plot it

% This script plot derivetive fd(x) of the function f(x).
% The script use the function_handle function to create numeric
% function ffd from dff(x).

pkg load symbolic
setenv PYTHON C:\ProgramData\Anaconda3\pythonw.exe

 % Declare symbolic variable x
syms x;

disp('The function f(x):')
% Input the function f(x) here:
f = sin(x) + cos(x)+ x*sin(x)

disp('The derivetive fd(x) of the function f(x):')
% The derivative of the function f:
% fd(x) = f'(x)
fd = diff(f, x)

% Convert symbolic function fd(x) to numeric function dff(x):
dff = function_handle(fd);

x=-2*pi:0.1:2*pi;
y= dff(x);
plot(x,y)
xlabel('x')
ylabel('fd(x)')

Output

>> plot_derive_sym
The function f(x):
f = (sym) x*sin(x) + sin(x) + cos(x)
The derivetive fd(x) of the function f(x):
fd = (sym) x*cos(x) + cos(x)

Graph of fd(x) = f'(x)
הוסף כיתוב




Octave - Use solve function to symbolic solution of equation whith one variable


% Script for solving equation with one variable using the solve function
% sytax :
% S = solve(eqn,var) solves the equation eqn for the variable var.

% load symbolic package, and initilize PYTHON variable. (Octave only).
pkg load symbolic
setenv PYTHON C:\ProgramData\Anaconda3\pythonw.exe

% Declare symbolic and variables and parameters
syms a b c x

% Define the equation to solve:
eqn = a*x^2 + b*x + c == 0

% activate solve function for x
S = solve(eqn, x)

% Solve for b:
Sb = solve(eqn, b)


Output








Octave - Create two Symbolic matrices and calculte thier product

The script declare symbolic variables: psi phi theta psidot phidot thetadot, and create 3 matrices RzPsi, RyTheta, RzPhiwith with the variables and print the matrices.
The script also calculate symbolic products of the matrices RzPhi * RyTheta,
and print the product. 

% script demonstrate how to use Symbolic matice

% load symbolic package, and initilize PYTHON variable. (Octave only).
pkg load symbolic
setenv PYTHON C:\ProgramData\Anaconda3\pythonw.exe

% Declare Symbolic variable
syms psi phi theta psidot phidot thetadot

% Define Symbolic matrices
RzPsi = [[cos(psi), -sin(psi), 0]; [sin(psi), cos(psi), 0]; [0,0,1]]
RyTheta = [[cos(theta), 0, sin(theta)];[0,1,0];[-sin(theta), 0, cos(theta)]]
RzPhi = [[cos(phi), -sin(phi), 0]; [sin(phi), cos(phi), 0]; [0,0,1]]

disp(' Calculate the symbolic products of: RzPhi * RyTheta')

% Multiply Symbolic Matrices
RzPhi * RyTheta


The script output:


Thursday, July 2, 2020

Matlab - Nonlinear Differential Equation with Initial Condition

Nonlinear Differential Equation with Initial Condition

Solve this nonlinear differential equation with an initial condition. The equation has multiple solutions.
 
Download the file nonldiff1.m

% Nonlinear Differential Equation with Initial Condition
%Solve this nonlinear differential equation with an initial condition.
%The equation has multiple solutions.

% initial conditions:  y(t0) = y0

syms y(t) t0 y0
ode = (diff(y,t)+y)^2 == 1;
cond = y(t0) == y0;
ySol(t) = dsolve(ode,cond)


The matlab output:
 ySol(t) =

 exp(-t)*exp(t0)*(y0 + 1) - 1
 exp(-t)*exp(t0)*(y0 - 1) + 1