I create this code to calculate something for me. At the end I need to compute the roots of Eq1 and Eq2, but because they are nonlinear I deside to compute their taylor expansion first and then compute their roots.
But I get error.
clc
clear
close all
format short g
syms x
%% Initilaizing
N=9;%input('Please enter the filter order: ');
Rl=20;%input('Please enter the value of Return Loss(RL) in dB: ');
IL=60;%input('Please enter the value of Insertion Loss(IL) in dB: ');
Fp=18;%input('Please enter the Band-Edge Frequency in GHz: ');
%% Definition Functions
Omega0=fminbnd(#(OM0)Func(OM0,Rl,IL,N),0,10,optimset('Tolx',1e-500));
epsilon=1/sqrt(10^(Rl/10)-1);
ILn=10^(0.1*IL);
TempFunc=#(x)real(1+epsilon^2*(cosh((N-3)*acosh(x*sqrt((Omega0^2 -1)/(Omega0^2 -x^2)))+3*acosh(x)))^2-ILn);
Omega1=fzero(TempFunc,1);
Frequency=[Omega0;Omega1;Omega0*Fp;Omega1*Fp;N];
Freq={'Infinite Attenuation(rad)';'Band-Edge Frequency(rad)';'Infinite Attenuation(GHz)';'Band-Edge Frequency(GHz)';'Order'};
Results=table(Freq,Frequency)
Eq1=#(x) 1+1j.*epsilon.*cosh((N-3).*acosh((x./1j).*sqrt((Omega0^2 -1)./(Omega0^2 -(x./1j).^2)))+3.*acosh(x./1j));
Eq2=#(x) 1-1j.*epsilon.*cosh((N-3).*acosh((x./1j).*sqrt((Omega0^2 -1)./(Omega0^2 -(x./1j).^2)))+3.*acosh(x./1j));
taylor(Eq1,'order',50)
This is the function that I use:
function Returned=Func(OM0,Rl,IL,N)
%% Main Section
epsilon=1/sqrt(10^(Rl/10)-1);
Omega=#(OM0) sqrt(OM0.^2+((N-3)/3).*OM0.*sqrt(OM0.^2 -1)) ;
Fn=#(OM0) real(cosh((N-3).*acosh(Omega(OM0).*sqrt((OM0.^2 -1)./(OM0.^2 -Omega(OM0).^2)))+3.*acosh(Omega(OM0))));
ILoss=#(OM0) real((1+epsilon^2.*Fn(OM0).^2));
[~,Temp]=fminbnd(ILoss,OM0,10,optimset('Tolx',1e-250));
Returned=abs(Temp-10^( IL/10));
end
This is the error:
Error using symengine (line 58)
Cannot compute a Taylor expansion of the input.
Error in sym/taylor (line 133)
tSym = mupadmex('symobj::taylor',f.s,x.s,a.s,options);
Error in OptimizingOmega0 (line 52)
taylor(Eq1,x,50)
Please help me how can I earn thr polynomial coefficients of Eq1,2 for computing roots.
Related
I am trying to solve this system of ODEs related to micro algae growth kinetics, I attach my function and script. I am getting some error related to the arguments. Please let me know where my error is thank you :) the system is composed of 7 ODEs and 7 corresponding state variables with 25 parameters
Function
function dx=odesys(t,var)
%dxdt=var(1);
%dldt=var(2);
%dsdt=var(3);
%dndt=var(4);
%gadt=var(5);
%Fadt=var(6);
%dhdt=var(7);
mumax=var(1);
Kxs=var(2);
Kixs=var(3);
Kxn=var(4);
Kixn=var(5);
qlmax=var(6);
Kls=var(7);
Kils=var(8);
Kinl=var(9);
Yxs=var(10);
Yxn=var(11);
Kh=var(12);
Yls=var(13);
Kxl=var(14);
Kixl=var(15);
Kli=var(16);
Kili=var(17);
sigma=var(18);
k1=var(19);
Kgas=var(20);
Kgan=var(21);
Kigan=var(22);
k2=var(23);
Kfas=var(24);
Kfan=var(25);
%Variable
%oilfree biomass--x(1)
%lipid production rate--x(2)
%Substrate consumption rate--x(3)
%N consumption rate--x(4)
%Byproduct GA--x(5)
%Byproduct FA--x(6)
%pH change--x(7)
%Rate equations
S=2.01; %g/L
N=0.098; %g/L
X=0.001; %g/L
Io=125*10-6; %E/m^2*s
l=1;
Il=Io*exp(-sigma*X*l);
mux=mumax*(S/(S+Kxs+(S^2/Kixs)))*(N/(N+Kxn+(N^2/Kixn)))*(Il/(Il+Kxl+(Il^2/Kixl)));
mul=(qlmax)*(S/(S+Kls+(S^2/Kils)))*(Kinl/(N+Kinl)*(Il/(Il+Kli+(Il^2/Kili))));
%ODE
dx(1)=(mux*X);
dx(2)=(mul*X);
dx(3)=(-(1/Yxs)*dx(1))-((1/Yls)*dx(2));
dx(4)=(-(1/Yxn)*dx(1));
dx(5)=(k1*(S/(S+Kgas))*(N/(N+Kgan+(N^2/Kigan))));
dx(6)=(k2*(S/S+Kfas)*(N/N+Kfan));
dx(7)=-Kh*dx(3);
% The function return value is always a vector length equal to number of
% equations in model, and has to be a column vector
dx=dx';
% dx=[dx(1);dx(2);dx(3);dx(4);dx(5);dx(6);dx(7)];
end
Script
clear all
clc
%Time span to solve ODE
tspan=[0 120];
int=[0,0,0,0,0,0,0];
%Conditions
var=[0.227;0.050;9.923;0.065;0.5;0.121;6.554;0.110;380.023;1.47;6.883;0.879;0.064;19.519;2053.924;15.023;2152.918;34.104;0.329;1.456;12.976;2.533;1.4055;12.976;2.533];
V = odesys(tspan(1), var);
iscolumn(V) % false
isrow(V) % true?
%Using ODE 45 to solve system
[t,X]=ode45(#odesys,tspan,int,var);
Error functions
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in run (line 17)
[t,X]=ode45(#odesys,tspan,int,var);
An easy way to do this is to define the 'var' variable inside your odesys function not in the script:
function dx=odesys(t, x)
%dxdt=var(1);
%dldt=var(2);
%dsdt=var(3);
%dndt=var(4);
%gadt=var(5);
%Fadt=var(6);
%dhdt=var(7);
var=[0.227;0.050;9.923;0.065;0.5;0.121;6.554;0.110;380.023;1.47;6.883;0.879;0.064;19.519;2053.924;15.023;2152.918;34.104;0.329;1.456;12.976;2.533;1.4055;12.976;2.533];
mumax=var(1);
Kxs=var(2);
...
Note the changes in the function declaration.
You have to add the x parameter here in function declaration. This is the variable which you are differentiating with respect to. You are not using it in your equations so you can name it anything but it should be included.
And finally, no need for the 'var' variable in the script as in here:
V = odesys(tspan(1));
and here:
[t,X]=ode45(#odesys,tspan,int);
Suppose we have the following function:
function f=lorenz(t,x,a,b,c)
% solve differential equation like this
%dx/dt=a*(y-x)
%dy/dt=-x*z+b*x-y
%dz/dt=xy-c*z/3
f=zeros(3,1);% preallocate result
f(1)=a*(x(2)-x(1));
f(2)=-x(1)*x(3)+b*x(1)-x(2);
f(3)=x(1)*x(2)-c*x(3)/3;
end
For running this program, let us use the following test file:
% test program
x0=[-2 -3.5 21];% initial point
a=input(' enter first coefficient : ');
b=input(' enter second coefficient: ');
c=input(' enter third coefficient : ');
[t,x]=ode45(#(x) lorenz(x,a,b,c),[0 10],x0);
plot(t,x(:,1),'r');
title(' solution of x part');
grid on
I have tried passing arguments to the function handle,
test_program
enter first coefficient : 10
enter second coefficient: 28
enter third coefficient : -8
but it gives me the following error:
Error using #(x)lorenz(x,a,b,c)
Too many input arguments.
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in test_program (line 6)
[t,x]=ode45(#(x) lorenz(x,a,b,c),[0 10],x0);
one solution involves using global variables, like this:
function f=lorenz(t,x)
% solve differential equation like this
%dx/dt=a*(y-x)
%dy/dt=-x*z+b*x-y
%dz/dt=xy-c*z/3
global a
global b
global c
f=zeros(3,1);% preallocate result
f(1)=a*(x(2)-x(1));
f(2)=-x(1)*x(3)+b*x(1)-x(2);
f(3)=x(1)*x(2)-c*x(3)/3;
end
but then it takes too long to run.
How else can I fix this issue? What I want is to pass different arguments, if I write inside code something like this
a=input('enter the coefficient : ')
then this will be repeated several times.
Don't use global variables.
The fix is very simple, add the t as input too:
[t,x] = ode45(#(t,x) lorenz(t,x,a,b,c),[0 10],x0);
I have code that, when run, should correctly use Lagrange Multipliers to find the maximum/minimum of a function here:
clear all
syms x y L;
f = x^4+2*y^4;
g = x^2+5*y^2+2*y^2-10;
firstpart=jacobian(f,[x y])-L*jacobian(g,[x y]);
[Lsoln,xsoln,ysoln]=solve(firstpart,x^2+5*y^2+2*y^2-10);
subs(f,{x,y},{xsoln,ysoln})
% The coordinates that correspond with the greatest and smallest values
% above are the maximum and minimum, respectively.
However, when I run it, I get four errors:
Error using sym.getEqnsVars>checkVariables (line 92) The second
argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 62)
checkVariables(vars);
Error in solve>getEqns (line 450) [eqns, vars] =
sym.getEqnsVars(argv{:});
Error in solve (line 225) [eqns,vars,options] = getEqns(varargin{:});
Could anyone help?
You are passing two equations as individual arguments zu solve, that is not possible. You have to put both into an array
[Lsoln,xsoln,ysoln]=solve([firstpart,x^2+5*y^2+2*y^2-10] );
I am going calculate the eigenvalues follow program:
I want to calculate the matrix H , and achieve eigenvalues.
acc=1.44e-10;
a=1.732*acc;
t=-2.550;
x1=0;
y1=0;
z1=0;
x2=acc*cos(60);
y2=acc*sin(60);
z2=0;
sh=0;
for i=-1:1
for j=-1:1
for k=1:2
sh=sh+1;
xk=acc*cos(60)*(k-1);
yk=acc*sin(60)*(k-1);
zk=0;
xx(sh)=(i*a)+(j*a/2)+xk;
yy(sh)=(sqrt(3)/2)*a*j+yk;
zz(sh)=zk;
ki(sh)=k;
R1(sh)=i;
R2(sh)=j;
end
end
end
L0=sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2);
r1=0:0.02:1;
kx=(2*pi/(sqrt(3)*a))*(1-r1);
ky=(2*pi/(3*a))*(1-r1);
for ik=1:50
for i=1:2
for j=1:sh
Dis(i+8,j)=sqrt((xx(i)-xx(j))^2+(yy(i)-yy(j))^2+(zz(i)-zz(j))^2);
if abs(Dis-L0)<0.1
Rx=R1(sh)*a+R2(sh)*a/2;
Ry=R2(sh)*sqrt(3)/2*a;
H(ki(i+8),ki(j))=H(ki(i+8),ki(j))+t*exp(1i*(kx(ik)*Rx+ky(ik)*Ry));
end
end
end
end
But I always get this error:
Error in (line 44)
H(ki(i+8),ki(j))=H(ki(i+8),ki(j))+t*exp(1i*(kx(ik)*Rx+ky(ik)*Ry));
Undefined function 'H' for input arguments of type 'double'.
How to solve this error?
If you initialize H in the beginning, the code works.
H = zeros(2,2);
Notice:
I do not know the way you are calculating the eigenvalues. You should use the predefined function eig to check your solution. Another ways is to use SVD to calculate the eigenvectors and eigenvalues.
I need to find roots of a function using Newton method. I enter interval and accuracy from keyboard. Here is my code
disp('Newton method')
fx=#(g) 5*sin(g.^3-2*g.^2-1);
fx1=#(g) 5*g*(3*g-4)*cos(-g.^3+2*g.^2+1);
fx2=inline('-5*((4-6*g)*cos(-g.^3+2*g.^2+1)-(4*g-3*g.^2).^2*sin(-g.^3+2*g.^2+1))');
e=input ('Enter accuracy:');
a=input ('enter a:');
b=input ('enter b:');
x0=a:e:b;
y= 5*sin(x0.^3-2*x0.^2-1);
y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1));
plot (x0,y),grid
xlabel('x'),ylabel('y')
fa=fx(a);
n=0;
if (fa*y2>0)
x1=a;
else
x1=b;
end;
while(abs(fx(x1))>e)
n=n+1;
x1=x1-(fx(x1))/(fx1(x1));
end;
disp(sprintf('Answer:%g',x1))
disp(sprintf('Number of iterations:%g',n))
When I compile, it says:
Error using *
Inner matrix dimensions must agree.
Error in Untitled3 (line 10)
y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1));
You are multiplying two 1xn vectors, that is not possible. This multiplication causes the error:
y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1));
^ ^
| |
Using element-wise multiplication .* might be the solution, but I don't know what you try to implement.