Matalab ODE with bad result - matlab

syms y(t) yef(t);
ymax=1200.40680;
yef(t)= 0.95(1/(1+(y(t)/ymax)y(t)+(1-(1/(1+(y(t)/ymax))ymax))));
yo=0.40680140;
k10=2.37;
R=0.00831447;
T=395;
HA=0.08;
n1=1.51;
E1=83.3;
K1=k10(10^10)(HA^n1)exp(-E1/RT);
ode=diff(y(t),t)==-K1yef(t);
cond=y(0)==yo;
ySol(t)=dsolve(ode,cond)
ySol(t) =
7119/125
Why i get this result? what im doing wrong?

Related

Matlab - vpasolve error when solving system of equations

I'm currently trying to solve a system of equations, where h2, eta, B, U are known but I have left them as symbolic variables for now:
clearvars
clc
syms x x0 h0 h1 h2 C2 eta B U
h(x) = h0 + h2*(x/(B/2))^2;
h_bar(x) = h(x)/h0;
hc_bar = 1+(h2/h0)*(x0/(B/2))^2;
x_bar(x) = x/(B/2);
x0_bar = x_bar(x0);
integration = int(((h_bar^2-hc_bar^2)/(h_bar^3)),x);
p(x) = ((3*eta*U*B)/(h0^2))*integration+C2;
p_d(x) = diff(p(x),x);
p_fun1 = p(x0_bar) == 0;
p_fun2 = p_d(x0_bar) == 0;
p_fun3 = p(-1) == 0;
p_fun4 = h1-h0 == h2;
Four equations, four unknowns so I should be able to solve this. However the system of equations is a bit overwhelming so I decided to try my luck with vpasolve:
vpasolve([p_fun1, p_fun2, p_fun3, p_fun4],[x0, h0, h1, C2])
This results in the following error:
Error using mupadengine/feval (line 166)
Symbolic parameters are not allowed in nonpolynomial equations.
Error in sym/vpasolve (line 172)
sol = eng.feval('symobj::vpasolve',eqns,vars,X0);
Error in Yke (line 35)
vpasolve([p_fun1, p_fun2, p_fun3, p_fun4],[x0, h0, h1, C2])
I have traced the error back to p_fun1 specifically, but I cannot see why this specific function would trigger the error. It is complicated, but p_fun2 is the derivative of p_fun1 and p_fun3 is simply p_fun1 with a different value substituted in the equation, yet these do not trigger the error.
I'm pretty sure the error is caused by replacing x with x_bar in the p_fun1 = p(x0_bar) == 0 call, but I don't know why and therefore I also don't know how to solve it. Does anyone see what I'm doing wrong at the moment?
vpasolve is a numerical solver (in fact, vpa stands for variable-precision arithmetic), and you cannot have symbolic parameters in the equations.
You should thus substitute numerical values to all the equation parameters.

Matlab non-linear ODEs system returns [empty sym]

I'm trying to solve this set of equations in matlab
syms x(t) y(t) z(t) g m h p wz wx wy
Dx=diff(x)
Dy=diff(y)
Dz=diff(z)
D2x=diff(x,2)
D2y=diff(y,2)
D2z=diff(z,2)
eq1=diff(x,2)==(-h*sqrt(diff(x).^2+diff(y).^2+diff(z).^2)*diff(x)+p* (wy.*diff(z)-wz*diff(y)))/m
eq2=diff(y,2)==(-h*sqrt(diff(x).^2+diff(y).^2+diff(z).^2)*diff(y)+p*(wz.*diff(x)-wx*diff(z)))/m
eq3=diff(z,2)==(-m*g-h*sqrt(diff(x).^2+diff(y).^2+diff(z).^2)*diff(z)+p*(wx.*diff(y)-wy*diff(x)))/m
g=9.81
m=2.5*10^-3
h=7.3*10^-4
p=1.8^-5
wz=-20*2*pi
wx=0.2*pi
wy=-20*2*pi
c1=x(0)==-0.5
c2=y(0)==-0.5
c3=z(0)==0.3
c4=Dx(0)==8
c5=Dy(0)==2
c6=Dz(0)==3
S=dsolve(eq1, eq2, eq3,c1,c2,c3,c4,c5,c6,t)
xSol(t)=S*x
ySol(t)=S*y
zSol(t)=S*z
but I get this
Warning: Explicit solution could not be found.
> In dsolve (line 201)
In Trajectoire (line 31)
S =
[ empty sym ]
I searched a solution on the forum but nothing really helps.
Can you help me to find out what the problem is ?
Here's my trial with ode45
g=9.81
m=2.5*10^-3
h=7.3*10^-4
p=1.8^-5
wz=-20*2*pi
wx=0.2*pi
wy=-20*2*pi
syms x(t) y(t) z(t)
V=odeToVectorField(diff(x,2)==(- h*sqrt(diff(x).^2+diff(y).^2+diff(z).^2)*diff(x)+p*(wy.*diff(z)-wz*diff(y)))/m, diff(y,2)==(-h*sqrt(diff(x).^2+diff(y).^2+diff(z).^2)*diff(y)+p*(wz.*diff(x)-wx*diff(z)))/m,diff(z,2)==(-m*g- h*sqrt(diff(x).^2+diff(y).^2+diff(z).^2)*diff(z)+p*(wx.*diff(y)-wy*diff(x)))/m)
M = matlabFunction(V,'vars', {'t','Y'})
[t,x,y,z]=ode45(M,[0 100],[8 2 3])

Error in using fzero

I am trying to solve a non-linear equation by using a while loop in Matlab but it's not giving me the results and give me an error in line that uses fzero function.
Why does this happens? How to solve the issue? Could someone help me with this?
Thanks in advance.
function z=optp(z,p)
z=3.067;
p=0;
while (z-p) > 0.8
syms a y Q p
p=z;
w=3;
C=1;
P=5;
b=1.2;
c=8;
B=0.5;
R=A^B*c*p^-b;
Q=(0.5-1/(p+5))*2400*((A^0.5)/(p^(6/5)));
x=Q;
J1=int(int(a*x*0.01,y,a*x/R,100),a,0,Q/x);
J2=(b*x^2*0.01/R)*int(a^2*1,a,0,Q/x);
J3=(P*R*b/p)*(int(int(y*0.01,y,a*x/R,100),a,0,Q/x));
J4=(b*R^2/x)*int(y^2*0.01,y,0,Q/R);
J5=(R*0.2)*(int(int(y*0.01,a,y*R/x,1),y,0,Q/R));
J=J1+J3+J4-J2-J5;
z=fzero(# (p) J,p)
end
end

Using solve instead of vpasolve to speed up calculations in Matlab?

I am relatively new to Matlab. At the moment I am solving an equation using vpasolve. Since that needs to loop over 80 variables, it is slow.
% input variables
% ffsample = Fourier transformed reference waveform
% ffref = Fourier transformed reference waveform
len=80;
n1=1;
n2=1.95;
alpha_grad=30;
beta_grad=14.858;
% constants
tp12=2.*n1.*cos(alpha)./(n2.*cos(alpha)+n1.*cos(beta));
tp21=2.*n2.*cos(beta)./(n1.*cos(beta)+n2.*cos(alpha));
%functions with unknown n3
syms n3
gamma= asin(n2.*sin(beta)./n3);
rp23 = (n3 .* cos(beta) - n2.*cos(gamma)) ./ (n3 .* cos(beta) + n2 .* cos(gamma));
t = cputime;
res_para=zeros(len,1);
%loop to solve for n3
digits(5):
for i=1:len
res_para(i)=vpasolve(ffsample(i)./ffref(i) == (tp12*tp21*rp23) ./ (tp12*tp21), n3);
end
e = cputime-t
%convert results to complex numbers
res_para=double(res_para);
res_para=squeeze(res_para);
res_para=res_para';
Now, I am trying to convert this into an equation which uses solve before entering the loop. As far as I understand it, I should use solve in conjunction with vpa? At the moment I am trying like the following code, but it wouldn't start:
res_p=solve(x == (tp12*tp21*rp23) ./ (tp12*tp21), n3);
t = cputime;
res_para=zeros(len,1);
for i=1:len
x=ffsample(i)./ffref(i);
res_para=vpa(res_p(x));
end
Any help would be greatly appreciated.
Update
Thank you for your replies. I changed the digits(5) position.
And yes, digits(5) is a very low value indeed however, it speeds teh above code up by approximately 3 times.
As I said, I am relatively new to matlab and thought that solving the equation symbolically first and then just insert the variables would greatly reduce the time, but maybe I am wrong here.
Considering I use vpasolve to solve a very simple equation numerically:
function [ xval ] = example( )
syms x
for y=1:10
xval(y) = vpasolve(y == 5.*x+6, x);
end
Wouldn't it be faster if I solve the function y=5x+6 symbolically for x first and then just include the msising values in the loop? Something like this:
function [ xval ] = example2( )
syms x
% symbolic solve for x (???)
% y=5x+6 ==> x=(y-6)/5
sol=solve( y == 5.*x+6, x)
for y=1:10
xval(y) = sol(y);
end
Obviously example2 does not work.

How to solve symbolic equation in matlab

I have an equation in Matlab according to X parameter . I want to find the amount of X for the random amounts of F(x) .
and I tried the code below . but It gives me two different results while my equation should have just one result .
even I tried the roots(f) instead of solve(f) but it gave me an error :
??? Undefined function or method 'isfinite' for input arguments of
type 'sym'.
anybody can help me in this ?
what should I do ?
even if I have a wrong idea about solving this problem please tell me .
Thank you
function betaDistribution_2(a,b)
syms x ;
y=inline((x^(a-1))*((1-x)^(b-1)));
beta=quad(y,0,1);
g=(1/beta)*(x^(a-1))*((1-x)^(b-1));
% I have this equation and I want to find the amount of x for the random
%amounts of p
p=int(g,x,0,x);
for i=0:50
fxi=rand(1);
f=p-fxi;
xi=solve(f);
result=eval(xi);
disp(result)
end
end
Try to filter your solutions.
a=1;
b=2;
% a threshold for imagery part
SMALL=1e-9;
syms x real;
y=inline((x^(a-1))*((1-x)^(b-1)));
beta=quad(y,0,1);
g=(1/beta)*(x^(a-1))*((1-x)^(b-1));
p=int(g,x,0,x);
% return true for physically meaningfull results
filter = #(xc) abs(imag(xc))<SMALL && real(xc)>0 && subs(g, x, xc) > 0 && subs(p, x, xc)>0;
for m=0:50
fxi=rand(1);
f=p-fxi;
xi=solve(f, x);
result=eval(xi);
idx = arrayfun (filter, result);
result = result(idx);
% make sure it is OK
assert(length(result)==1);
disp(result)
end