I have a sort of obscure question. I am wanting to do something with two separate set of syms and manipulate the syms. This is a little difficult for me to explain without including my code firsts so:
syms x Wg Iyy b
S = syms;
Pz1 = (Wg*n)/(2*b);
Vzx1 = int(Pz1,x,x,b)
Myx1 = -int(Vzx1,x,x,b)
dw_dx = (-1/(E*Iyy))*int(Myx1,x,0,x);
wx1 = int(dw_dx,x,0,x)
% Get the numerical output of the value using b as wing
clear S;
% assume(S,'clear');
% cellfun(#clear, S);
L = 6;
b = (1490+10*L)/2;
Vzx1_num = Vzx1
Myx1_num = Myx1
wx1_num = wx1
% need to sub b for b=b_prime*cos(40)
% Not really sure how to do this, I know I have to redclare the syms
syms x Wg Iyy
So to expand on more what is going on and what I am looking to do. The first syms (syms = x Wg Iyy b), I would like to keep as is because based on my needs I want to output the value of Vzx1 in terms of those variables. This part works perfectly for what I am intending to do. The error lies in how I would like to manipulate the value I have generated. So secondly, I would like to ouput the numerical value of these generated expression by using the value of b. I am trying to clear the value of syms.
When using
S= syms
clear S
The value of S gets clear. So b would no longer be a symbol and I can set the value of b, allowing Vzx1_num to ouput the value of Vzx1 with b replaced with a number. This is not the case as b remains a sym
Then thirdly I want to substitute the variable value of b for the value of b_prime*cos(40). I have tried to mess with this but based on how I have previously declared b as a syms I am having a hard time manipulating this variable. So when I use syms x Wg Iyy. That is not the value of syms the value of syms outputs
syms
Your symbolic variables are:
Iyy Myx1_num Vzx1 Wg wx1 x
Myx1 Pz1 Vzx1_num dw_dx wx1_num
I am extremely confused why syms gets built using the values I have manipulated previously.
Any and all help would be greatly appreciated.
Related
I originally asked this question yesterday and found the answer myself; however, I used the clear all command in Matlab and now the function throws an error Undefined function or variable 'y'.
I used the code from my answer
function [s1] = L_Analytic3(eqn,t0,h,numstep,y0)
%Differential Equation solver for specific inputs
% eqn is the differential equation
% t0 is start of evaluation interval
% h is stepize
% numstep is the number of steps
% y0 is the initial condition
syms y(x)
cond = y(0) == y0;
A = dsolve(eqn, cond);
s1 = A;
S1 = s1;
for x = t0 : h : h*(numstep)
subs(x);
if x == t0
S1 = subs(s1,x);
else
S1 = [subs(S1), subs(s1,vpa(x))];
end
end
end
and also put L_Analytic3(diff(y) == y,0,0.1,5,1) into the Command Window after entering clear all. I have to run a seperate code
syms y(x)
cond = y(0) == 1;
A = dsolve(diff(y) == y, cond);
before using my function in order for the function to work. Is this just because A,ans,cond,x, and y, are already defined by the 3 line code before using the function? If so, is there a way that I can use the function without having to use that 3 line code first?
When you do L_Analytic3(diff(y) == ...); you do not have variable y defined, so MATLAB complains - it has no way of knowing y is a symbol that will be defined in the function you are calling. You do not require all 3 lines of code. syms y(x) should be enough to define y and lets you use the function call you wanted.
Now, there are 2 easy ways to fix this that I see:
A script (or another function) that has syms y(x), followed by the call to L_Analytic3 the way you are doing it (which now does not need syms y(x), it has been defined already).
Give anonymous equation as the input instead, say #(x) diff(x)==x, and change a line of L_Analytic3 slightly to A = dsolve(eqn(y), cond);
Both ways work fine for this, no idea if 2nd one breaks in more complex cases. I would likely pick 1st version if you are doing symbolic stuff, and 2nd if you would like to have same function call to both numeric and symbolic functions.
I need to generate two chaotic sequences based on chen's hyperchaotic system.It has to be generated from the following four formulas
X=ay-x;
Y=-xz+dx+cy-q;
Y=xy-bz;
Q=x+k;
where a,b,c,d,x,y,z,q are all initialised as follows.
I need only X and Y
where
X=[x1,x2,...x4n]
Y=[y1,y2,...y4n]
a=36 ;
b=3 ;
c=28 ;
d=16 ;
k=0.2 ;
x=0.3 ;
y=-0.4 ;
z=1.2 ;
q=1 ;
n=256 ;
I tried the following code but i'm not able to get it properly.
clc
clear all
close all
w=imread('C:\Users\Desktop\a.png');
[m n]=size(w)
a=36;
b=3;
c=28;
d=16;
k=0.2;
x(1)=0.3;
y(1)=-0.4;
z(1)=1.2;
q(1)=1;
for i=1:1:4(n)
x(i+1)=(a*(y(i)-x(i)));
y(i+1)=-(x(i)*z(i))+(d*x(i))+(c*y(i))-q(i);
z(i+1)=(x(i)*y(i))-(b*z(i));
q(i+1)=x(i)+k;
end
disp(x);
disp(y);
pls help. thanks in advance.
Your code isn't even close to doing what you want it to. Fortunately, I'm vaguely interested in the problem and I have a bunch of spare time, so I thought I'd try and implement it step by step to show you what to do. I've left a few gaps for you to fill in.
It sounds like you want to integrate the hyperchaotic chen system, which has various definitions online, but you seem to be focusing on
So let's write a matlab function that defines that system
function vdot = chen(t, v, a, b, c, d, k)
% Here you unpack the input vector v -
x = v(1); y = v(2); z = v(3); q = v(4);
% Here you need to implement your equations as xdot, ydot etc.
% xdot = ...
% ydot = ...
% I'll leave that for you to do yourself.
% Then you pack them up into an output vector -
vdot = [xdot; ydot; zdot; qdot];
end
Save that in a file called chen.m. Now you need to define the values of the parameters a, b, c, d and k, as well as your initial condition.
% You need to define the values of a, b, c, d, k here.
% a = ...
% b = ...
% You also need to define the vector v0, which is a 4x1 vector of your
% initial conditions
% v0 = ...
%
This next line creates a function that can be used by Matlab's integration routines. The first parameter t is the current time (which you don't actually use) and the second parameter is a 4x1 vector containing x, y, z, q.
>> fun = #(t,v) chen(t,v,a,b,c,d,k)
Now you can use ode45 (which does numerical integration using a 4th order runge-kutta scheme) to integrate it and plot some paths. The first argument to ode45 is the function you want to be integrated, the second argument is the timespan to be integrated over (I chose to integrate from 0 to 100, maybe you want to do something different) and the third argument is your initial condition (which hopefully you already defined).
>> [t, v] = ode45(fun, [0 100], v0);
The outputs are t, a vector of times, and v, which will be a matrix whose columns are the different components (x, y, z, q) and whose rows are the values of the components at each point in time. So you can pull out a column for each of the x and y components, and plot them
>> x = v(:,1);
>> y = v(:,2);
>> plot(x,y)
Which gives a reasonably chaotic looking plot:
#Abirami Anbalagan and Sir #Chris Taylor, I have also studied hyperchaotic system up to some extent. According to me, for system to be chaotic, values should be like
a= 35; b= 3; c= 12; d= 7;
v(n) = [-422 -274 0 -2.4]transpose
where v(n) is a 4*1 Matrix.
I'm working with MATLAB's symbolic toolbox, and I'm having some issues pulling out the coefficients of derivatives. Maybe MATLAB can't do what I am looking for. Anyway, code that reproduces the issue I'm having is shown below:
clear ; close all; clc;
syms a b t
x = sym('x(t)');
y = sym('y(t)');
syms a b;
ra = a*cos(x);
radot = diff(ra, t);
xdot = diff(x,t);
ydot = diff(y,t);
% This one works as expected
works = coeffs(radot(1), xdot)
% This doesn't work as expected
fails = coeffs(radot(1), ydot)
Comments in the above code sections highlight what works and what does not work as expected. Specifically, the outputs are:
radot =
-a*sin(x(t))*diff(x(t), t)
works =
-a*sin(x(t))
fails =
-a*sin(x(t))*diff(x(t), t)
Does anyone know why this happens or whether I'm doing something wrong?
The result of the last line is constant with respect to ydot, and therefore the whole expression is seen as a single coefficient (a constant).
What is your expected result for coeffs(radot(1), ydot)?
It looks like you may be using coeffs for something that it wasn't meant for. Look at the help. It's designed to give the coefficients of a polynomial, not wether a differential equation is a function of one variable or another.
If you happen to be trying to take the derivative with respect to xdot and ydot, you can do this
syms z; % Subsitution variable for diff(x(t), t) and diff(y(t), t)
diff(subs(radot(1),xdot,z),z)
diff(subs(radot(1),ydot,z),z)
which returns
ans =
-a*sin(x(t))
ans =
0
We have an equation similar to the Fredholm integral equation of second kind.
To solve this equation we have been given an iterative solution that is guaranteed to converge for our specific equation. Now our only problem consists in implementing this iterative prodedure in MATLAB.
For now, the problematic part of our code looks like this:
function delta = delta(x,a,P,H,E,c,c0,w)
delt = #(x)delta_a(x,a,P,H,E,c0,w);
for i=1:500
delt = #(x)delt(x) - 1/E.*integral(#(xi)((c(1)-c(2)*delt(xi))*ms(xi,x,a,P,H,w)),0,a-0.001);
end
delta=delt;
end
delta_a is a function of x, and represent the initial value of the iteration. ms is a function of x and xi.
As you might see we want delt to depend on both x (before the integral) and xi (inside of the integral) in the iteration. Unfortunately this way of writing the code (with the function handle) does not give us a numerical value, as we wish. We can't either write delt as two different functions, one of x and one of xi, since xi is not defined (until integral defines it). So, how can we make sure that delt depends on xi inside of the integral, and still get a numerical value out of the iteration?
Do any of you have any suggestions to how we might solve this?
Using numerical integration
Explanation of the input parameters: x is a vector of numerical values, all the rest are constants. A problem with my code is that the input parameter x is not being used (I guess this means that x is being treated as a symbol).
It looks like you can do a nesting of anonymous functions in MATLAB:
f =
#(x)2*x
>> ff = #(x) f(f(x))
ff =
#(x)f(f(x))
>> ff(2)
ans =
8
>> f = ff;
>> f(2)
ans =
8
Also it is possible to rebind the pointers to the functions.
Thus, you can set up your iteration like
delta_old = #(x) delta_a(x)
for i=1:500
delta_new = #(x) delta_old(x) - integral(#(xi),delta_old(xi))
delta_old = delta_new
end
plus the inclusion of your parameters...
You may want to consider to solve a discretized version of your problem.
Let K be the matrix which discretizes your Fredholm kernel k(t,s), e.g.
K(i,j) = int_a^b K(x_i, s) l_j(s) ds
where l_j(s) is, for instance, the j-th lagrange interpolant associated to the interpolation nodes (x_i) = x_1,x_2,...,x_n.
Then, solving your Picard iterations is as simple as doing
phi_n+1 = f + K*phi_n
i.e.
for i = 1:N
phi = f + K*phi
end
where phi_n and f are the nodal values of phi and f on the (x_i).
my question relates to the Symbolic Math Toolbox from Matlab. I have the following code:
syms x x_0 u delta sigma_1
mu = sym ('mu(x)');
sigma_u = sym ('sigma(u)');
sigma = sym ('sigma(x)');
f = int (1/sigma_u, u, x_0, x);
df = subs(diff(f,x))
df_2 = subs(diff (f,x,2))
L = subs(mu*df+1/2*sigma^2*df_2)
The result of L is corect
L =
mu(x)/sigma(x) - diff(sigma(x), x)/2
However, for further derivations and for simplicity, I would like to define
sigma_1 = sym('diff(sigma,x)');
or in a similar way such as to get as result for
L =
mu(x)/sigma(x) - sigma_1(x)/2
Basically, I would like to store under a name the symbolic expression diff(sigma(x),x) such that Matlab knows that when it gets this result in a expression, to poste the name sigma_1 (x) instead of diff(sigma(x),x)
Yes it is possible, you can use subs(L, 'diff(sigma(x),x)', 'sigma_1(x)'). Note to make the substitution work, the second input of subs must be exactly like what you want to replace; hence it cannot be 'diff(sigma, x)' which lacks the (x) behind the sigma.
Also note that here is a similar question for which I provided a more complete solution (they asked the question after yours, but I read theirs first).