Showing posts with label symbolic. Show all posts
Showing posts with label symbolic. Show all posts

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








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


Wednesday, July 1, 2020

Demo of inputting a function at the input prompt

The script get function at the prompt and calculate the first and the second order derivative
of the function. The script can be run on Octave platform.
Download the file demo_prompt_function.m

%     Demo of inputting a function at the input prompt
%     and making an Anonymous function.

% This program. shows how to take a
% string input and make it into an anonymous function
% this uses the symbolic pkg.
% Load Symbolic pkg (Octave only)
pkg load symbolic
setenv PYTHON C:\ProgramData\Anaconda3\pythonw.exe

disp("Example input")
disp("x^2 + 3*x - 1 + 5*x*sin(x)")
str_fucn=input("please enter your function  ","s")
fucn_sym=sym(str_fucn)
f=function_handle(fucn_sym)
% now back to symbolic
syms x;
ff=formula(f(x));
% now calculate the derivative of the function
ffd=diff(ff);
% and convert it back to an Anonymous function
df=function_handle(ffd)
% now lets do the second derivative
ffdd=diff(ffd);
ddf=function_handle(ffdd)
% and now plot them all at interva (-2) - 2
x1=-2:.001:2;
plot(x1,f(x1),x1,df(x1),x1,ddf(x1))
grid minor on
legend("f","f '", "f '' ")

The result for input function cos(x)

Example input
x^2 + 3*x - 1 + 5*x*sin(x)
please enter your function  cos(x)
str_fucn = cos(x)
fucn_sym = (sym) cos(x)
f =

@(x) cos (x)

df =

@(x) -sin (x)

ddf =

@(x) -cos (x)
The Graph





Octave - Calculate argument in Symbolic function

The script convert symbolic expression df(x) to funtion dfh(x) using the
 function_handle Octave function. The script can run on Octave platform.
Download the file demo_sym.m

% Demo of how to use a number (which was calculated in an octave
% variable) in a symbolic calculation, without getting a warning.

% use octave to calculate some number:
 a = pi/2

% now do some work with the symbolic pkg
 syms x
 f = x * cos (x)
 df = diff (f)

% Now we want to evaluate df at a:

 % subs (df, x, a)     # this gives the "rats" warning (and gives a symbolic answer)

% So instead, try

 dfh = function_handle (df)

 disp('dfh(a) = ')
 dfh (a)

% ans = -1.5708


% And you can evaluate dfh at an array of "double" values:

disp ('dfh ([1.23 12.3 pi/2]) = ')
 dfh ([1.23 12.3 pi/2])

% ans =
%  -0.82502   4.20248  -1.57080


The Result

a =  1.5708
f = (sym) x*cos(x)
df = (sym) -x*sin(x) + cos(x)
dfh =

@(x)   -x .* sin (x) + cos (x)

dfh(a) =
ans = -1.5708
dfh ([1.23 12.3 pi/2]) =
ans =

  -0.82502   4.20248  -1.57080

Matlab - Symbolic mulitiplication matrice by operator

The Script multiply operator matrice U by matrice A . The script run on octave platform.
Download the file matrice1.m

% This script define symbolic  4*4 matrix A
% The script define matrix U as opertor
% The script solve the multilication U*A

% Load symbolic package, and initilize PYTHON variable.
pkg load symbolic
setenv PYTHON C:\ProgramData\Anaconda3\pythonw.exe

% Define Symbolic variables
syms x11 x12 x13 x14 x21 x22 x23 x24
syms x31 x32 x33 x34 x41 x42 x43 x44

% Matrice A
A = [ x11 x12 x13 x14
x21 x22 x23 x24
x31 x32 x33 x34
x41 x42 x43 x44 ]

% Operator Matrice U
U = [ 0 0 0 0
1 0 0 0
1 1 0 0
0 0 0 0 ]

disp('C = U*A')
 C = U*A



The result

Matlab - Symbolic mulitiplication matrice by operator





Featured Post

Solve simple Absolute Value Inequality

  Exam - BARTON COLLEGE PRACTICE PLACEMENT TEST Ex. 39 Exercise: The inequality |8 - x| < 8   is equivalent to:   a) x < 0 b) x ...