Solving an equation in Matlab
显示 更早的评论
Dear, I'm trying to resolve this equation for my thesis project, u and v are the values that I try to calculate, the only parameter that change is the Tx and Ty, all the other parameters are constant. I appreciate any help

tx = [0.1 0.5 0.12 0.14 0.22 0.1 0.12 0.29]
ty = [0.4 0.12 0.34 0.14 0.13 0.03 0.2 0.13]
Omega = 7.3E-5;
latitude = -12;
f0 = 2*Omega*sind(latitude);
r = 1./(2.*24.*3600)
Hm = 25
rho = 1.025e3
dvdt0 = -r.*v0 - f0.*u0 -ty./(rho.*Hm);
dudt0 = -r.*u0 + f0.*v0 + tx./(rho.*Hm);
采纳的回答
These have analytic solutions:
syms u(t) v(t) f taux tauy Hm rho R u0 v0
du = diff(u);
dv = diff(v);
DE = [du - f*v == taux/(Hm*rho) - R*u; dv - f*u == taux/(Hm*rho) - R*v]
Soln = dsolve(DE, u(0)==u0, v(0)==v0)
u_Soln = Soln.u;
v_Soln = Soln.v;
u_Soln = simplify(u_Soln, 'Steps',500)
v_Soln = simplify(v_Soln, 'Steps',500)
u_fcn = matlabFunction(u_Soln)
v_fcn = matlabFunction(v_Soln)
Note that ‘u_fcn’ and ‘v_fcn’ are anonymous functions you can use outside of the Symbolic Math Toolbox.
10 个评论
Dear Star Stride, thanks you so much for your help and time, I'm trying to understand the error in the line Soln = dsolve(DE, u(0)==u0, v(0)==v0), I will apreciate any help.
Error using mupadengine/feval (line 163)
The equations are invalid.
Error in dsolve>mupadDsolve (line 336)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 193)
sol = mupadDsolve(args, options);
clear all
close all
taux = [0.1 0.5 0.12 0.14 0.22 0.1 0.12 0.29]
tauy = [0.4 0.12 0.34 0.14 0.13 0.03 0.2 0.13]
Omega = 7.3E-5;
latitude = -12;
f = 2*Omega*sind(latitude);
R = 1./(2.*24.*3600)
Hm = 25
rho = 1.025e3
%----------------------------
syms u(t) v(t) f taux tauy Hm rho R u0 v0
du = diff(u);
dv = diff(v);
DE = [du - f*v == taux/(Hm*rho) - R*u; dv + f*u == tauy/(Hm*rho) - R*v]
% dvdt0 = -r.*v0 - f.*u0 -taux./(rho.*H);
% dudt0 = -r.*u0 + f.*v0 +tauy./(rho.*H);
% initial conditions for analytic solution
% u0 = 0;
% v0 = 0;
Soln = dsolve(DE, u(0)==u0, v(0)==v0)
Error using mupadengine/feval (line 163)
The equations are invalid.
Error in dsolve>mupadDsolve (line 336)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 193)
sol = mupadDsolve(args, options);
u_Soln = Soln.u;
v_Soln = Soln.v;
u_Soln = simplify(u_Soln, 'Steps',500)
v_Soln = simplify(v_Soln, 'Steps',500)
u_fcn = matlabFunction(u_Soln)
v_fcn = matlabFunction(v_Soln)
I have no problems with it in R2019b.
This code:
syms u(t) v(t) f taux tauy Hm rho R u0 v0
du = diff(u);
dv = diff(v);
DE = [du - f*v == taux/(Hm*rho) - R*u; dv - f*u == taux/(Hm*rho) - R*v]
Soln = dsolve(DE, u(0)==u0, v(0)==v0)
u_Soln = Soln.u;
v_Soln = Soln.v;
u_Soln = simplify(u_Soln, 'Steps',500)
v_Soln = simplify(v_Soln, 'Steps',500)
u_fcn = matlabFunction(u_Soln)
v_fcn = matlabFunction(v_Soln)
produces these results:
u_Soln =
exp(-t*(R + f))*(u0/2 - v0/2) - (exp(-t*(R - f))*(2*taux - 2*taux*exp(t*(R - f)) - Hm*R*rho*u0 - Hm*R*rho*v0 + Hm*f*rho*u0 + Hm*f*rho*v0))/(2*Hm*rho*(R - f))
v_Soln =
- exp(-t*(R + f))*(u0/2 - v0/2) - (exp(-t*(R - f))*(2*taux - 2*taux*exp(t*(R - f)) - Hm*R*rho*u0 - Hm*R*rho*v0 + Hm*f*rho*u0 + Hm*f*rho*v0))/(2*Hm*rho*(R - f))
u_fcn = @(Hm,R,f,rho,t,taux,u0,v0)exp(-t.*(R+f)).*(u0./2.0-v0./2.0)-(exp(-t.*(R-f)).*(taux.*2.0-taux.*exp(t.*(R-f)).*2.0-Hm.*R.*rho.*u0-Hm.*R.*rho.*v0+Hm.*f.*rho.*u0+Hm.*f.*rho.*v0))./(Hm.*rho.*(R-f).*2.0)
v_fcn = @(Hm,R,f,rho,t,taux,u0,v0)-exp(-t.*(R+f)).*(u0./2.0-v0./2.0)-(exp(-t.*(R-f)).*(taux.*2.0-taux.*exp(t.*(R-f)).*2.0-Hm.*R.*rho.*u0-Hm.*R.*rho.*v0+Hm.*f.*rho.*u0+Hm.*f.*rho.*v0))./(Hm.*rho.*(R-f).*2.0)
I edited the output slightly to make it readable.
Dear Star Strider, thank for your time. I run the code, but the initial conditions added in the dsolve code it seems to not work in my version of matlab 2016a (without the initial conditions works), i found that I can add the initial conditions like:
cond = [u(0) == u0,v(0)==v0];
sol = dsolve(DE,cond)
but also did not work..
>> Soln = dsolve(DE, u(0)==0, v(0)==0)
Error using mupadengine/feval (line 163)
The equations are invalid.
Error in dsolve>mupadDsolve (line 336)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 193)
sol = mupadDsolve(args, options);
>> Soln = dsolve(DE)
Soln =
v: [1x1 sym]
u: [1x1 sym]
>> Soln.u
ans =
exp(-t*(R - f))*(C2 + (taux*exp(R*t - f*t))/(Hm*rho*(R - f))) - C1*exp(-t*(R + f))
Do not use the ‘taux’ and ‘tauy’ data (or any of the others) when calculating the solutions to the differential equations. Leave them until the functions are evaluated.
Try this (comment-out the symbolic derivations and resulting functions first):
u_fcn = @(Hm,R,f,rho,t,taux,tauy,u0,v0)exp(-t.*(R-f)).*(u0./2.0+v0./2.0-(taux+tauy)./(Hm.*rho.*(R-f).*2.0)+(exp(t.*(R-f)).*(taux+tauy))./(Hm.*rho.*(R-f).*2.0))+exp(-t.*(R+f)).*(u0./2.0-v0./2.0-(taux-tauy)./(Hm.*rho.*(R+f).*2.0)+(exp(t.*(R+f)).*(taux-tauy))./(Hm.*rho.*(R+f).*2.0));
v_fcn = @(Hm,R,f,rho,t,taux,tauy,u0,v0)exp(-t.*(R-f)).*(u0./2.0+v0./2.0-(taux+tauy)./(Hm.*rho.*(R-f).*2.0)+(exp(R.*t-f.*t).*(taux+tauy))./(Hm.*rho.*(R-f).*2.0))-exp(-t.*(R+f)).*(u0./2.0-v0./2.0-(taux-tauy)./(Hm.*rho.*(R+f).*2.0)+(exp(R.*t+f.*t).*(taux-tauy))./(Hm.*rho.*(R+f).*2.0));
taux = [0.1 0.5 0.12 0.14 0.22 0.1 0.12 0.29]
tauy = [0.4 0.12 0.34 0.14 0.13 0.03 0.2 0.13]
Omega = 7.3E-5;
latitude = -12;
f = 2*Omega*sind(latitude);
R = 1./(2.*24.*3600);
Hm = 25;
rho = 1.025e3;
u0 = 0; % Substitute Correct Initial Condition
v0 = 0; % Substitute Correct Initial Condition
[Taux,Tauy] = ndgrid(taux, tauy); % Create Matrices From The Vectors
tv = linspace(0, 100, 3);
u_fcnt = @(t) u_fcn(Hm,R,f,rho,t,Taux,Tauy,u0,v0); % Function Only Of ‘t’ Here
v_fcnt = @(t) v_fcn(Hm,R,f,rho,t,Taux,Tauy,u0,v0); % Function Only Of ‘t’ Here
u = u_fcnt(1);
v = v_fcnt(1);
figure
hold on
for k = 1:numel(tv)
surf(taux, tauy, u_fcnt(tv(k)))
end
hold off
grid on
view(30, 30)
xlabel('\tau_x')
ylabel('\tau_y')
zlabel('u')
figure
hold on
for k = 1:numel(tv)
surf(taux, tauy, v_fcnt(tv(k)))
end
hold off
grid on
view(30, 30)
xlabel('\tau_x')
ylabel('\tau_y')
zlabel('\nu')
Also, there was an error. The correct expression for ‘DE’ is:
DE = [du - f*v == taux/(Hm*rho) - R*u; dv - f*u == tauy/(Hm*rho) - R*v]
I already corrected for that in the code for ‘u_fcn’ and ‘v_fcn’ and the rest in the code in this Comment.
Note: The plots appear to be a bit strange, because ‘taux’ and ‘tauy’ have repeated elements, and they are not sorted. You may not want (or need) to plot them, hoiwever this code demonstrates the correct way to evaluate them. They are functions of three variables
, so that must be accounted for in evaluating them.
Thanks Star, I notice about the error, the other error is the sing dv + f*u (can you please add the u_fcn and v_fcn solutions), I try to fixing but i can get the dsolve works, I definitely going to update Matlab to 2019, I really apreciate all your help (I'm learning a lot in the process). At the end my porpose is to compare the vector u and v, of this model with real insitu data.

% DE = [du - f*v == taux/(Hm*rho) - R*u; dv + f*u == tauy/(Hm*rho) - R*v]
clear all
close all
syms u(t) v(t) f taux tauy Hm rho R u0 v0
du = diff(u);
dv = diff(v);
DE = [du - f*v == taux/(Hm*rho) - R*u; dv + f*u == tauy/(Hm*rho) - R*v]
Soln = dsolve(DE, u(0)==u0, v(0)==v0)
Error using mupadengine/feval (line 163)
The equations are invalid.
Error in dsolve>mupadDsolve (line 336)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 193)
sol = mupadDsolve(args, options);
u_Soln = Soln.u;
v_Soln = Soln.v;
u_Soln = simplify(u_Soln, 'Steps',500)
v_Soln = simplify(v_Soln, 'Steps',500)
u_fcn = matlabFunction(u_Soln)
v_fcn = matlabFunction(v_Soln)
What does ‘... the other error is the sing dv + f*u ...’ mean? I coded the equations as written.

DE = [du - f*v == taux/(Hm*rho) - R*u; dv "+" f*u == tauy/(Hm*rho) - R*v] right?
------
% your comment
Also, there was an error. The correct expression for ‘DE’ is:
DE = [du - f*v == taux/(Hm*rho) - R*u; dv "-" f*u == tauy/(Hm*rho) - R*v]
That is what I coded, yes.
Thanks Star, you helping me a lot, now im going to try to make the dsolve works, in that way I can add more terms to try new solutions, again, thanks you so much for your time... best regards.
syms u(t) v(t) f taux tauy Hm rho R u0 v0
du = diff(u);
dv = diff(v);
DE = [du - f*v == taux/(Hm*rho) - R*u; dv + f*u == tauy/(Hm*rho) - R*v]
Soln = dsolve(DE, u(0)==u0, v(0)==v0)
u_Soln = Soln.u;
v_Soln = Soln.v;
u_Soln = simplify(u_Soln, 'Steps',500)
v_Soln = simplify(v_Soln, 'Steps',500)
u_fcn = matlabFunction(u_Soln)
v_fcn = matlabFunction(v_Soln)
As always, my pleasure!
I will help as I can.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Structural Mechanics 的更多信息
产品
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
