Solving an equation in Matlab
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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);
채택된 답변
Star Strider
2020년 2월 12일
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.
Pablo Estuardo
2020년 2월 13일
편집: Pablo Estuardo
2020년 2월 13일
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)
Star Strider
2020년 2월 13일
What does ‘... the other error is the sing dv + f*u ...’ mean? I coded the equations as written.
Pablo Estuardo
2020년 2월 13일
편집: Pablo Estuardo
2020년 2월 13일

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]
Star Strider
2020년 2월 13일
That is what I coded, yes.
Pablo Estuardo
2020년 2월 13일
편집: Pablo Estuardo
2020년 2월 13일
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)
Star Strider
2020년 2월 13일
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)
