How to solve the error while solving the given equation by ode45 or ode15s?
1 回表示 (過去 30 日間)
古いコメントを表示
clear all;
clc;
%initialisation
delr=0.0004;
tspan =[0 50];
k= 0.8
rho = 0.5
a=2;
b=3;
c=4;
d=5;
y0=0.5;
i=2;j=2;
[t,T] = ode15s(@(t,T) dTdt, tspan, y0);
k4(i,j)= lambda(i,j)/r(i,j)*(delr)*(rho)*(Cp);
Tin=500;
function dTdt = odeFunc(t,T)
lambda(i,j)= 0.9*T;
r(i,j)=(j-1.5)*delr;
cp=a+b*T+c*T^2+d*T^3;
dTdt(i,j) = k*(Tin(i,j)-k*T(i,j))+(k4(i,j))*T(i,j+1))-k4(i,j)*T(i,j);
end
[SL: formatted code as code not text]
I am getting error message : Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
how to solve such errors?
0 件のコメント
回答 (2 件)
VBBV
2023 年 4 月 9 日
編集済み: VBBV
2023 年 4 月 9 日
As the expression states an extra parenthesis is included in this line
dTdt(i,j) = k*(Tin(i,j)-k*T(i,j))+(k4(i,j))*T(i,j+1))-k4(i,j)*T(i,j);
%^ an extra parenthesis
try with below after removing that extra parenthesis
% try with the below
dTdt(i,j) = k*(Tin-T(i,j))+(k4(i,j)*T(i,j+1))-k4(i,j)*T(i,j);
2 件のコメント
VBBV
2023 年 4 月 9 日
編集済み: VBBV
2023 年 4 月 9 日
Please rename your file to something other than standard Matlab functions e.g. ode15s which you are using already to evaluate the equations. It is not recommended by Matlab to use variable names and /or filenames in your program as standard Matlab function names since they conflict with the execution and becomes difficult to debug if program is big.
While renaming the filenames please follow these essential rules given in this page
You can also refer the same from a standard Mathworks page where it's given in detail.
Steven Lord
2023 年 4 月 9 日
There are several problems with this code.
clear all;
clc;
These two lines are unnecessary. Often people put them in because they've seen them in others' scripts but they can impede the ability to debug the code efficiently.
%initialisation
delr=0.0004;
tspan =[0 50];
k= 0.8
rho = 0.5
a=2;
b=3;
c=4;
d=5;
y0=0.5;
i=2;j=2;
[t,T] = ode15s(@(t,T) dTdt, tspan, y0);
Nowhere in this code do you define a function named dTdt that accepts 0 input arguments and returns the value of the right-hand side of your system of ODEs. If you want this to call your function named odeFunc with two inputs use either:
[t,T] = ode15s(@(t,T) odeFunc(t, T), tspan, y0);
or
[t,T] = ode15s(@odeFunc, tspan, y0);
k4(i,j)= lambda(i,j)/r(i,j)*(delr)*(rho)*(Cp);
Tin=500;
function dTdt = odeFunc(t,T)
lambda(i,j)= 0.9*T;
Nowhere in this function are variables i or j defined, so MATLAB will use the values returned by the i and j functions. This will error because you can't assign to the sqrt(-1)th row or column of a matrix. Yes, you defined the i and j variables before you called ode15s but that is a separate workspace. Since you didn't pass the i and j variables into the function call's workspace, they're not visible in the function call's workspace. [I'm ignoring nested functions and global variables for purpose of this explanation.]
r(i,j)=(j-1.5)*delr;
Again, since you didn't pass delr into this function's workspace, it's not visible. For ways to pass additional parameters into the function see this documentation page.
cp=a+b*T+c*T^2+d*T^3;
dTdt(i,j) = k*(Tin(i,j)-k*T(i,j))+(k4(i,j))*T(i,j+1))-k4(i,j)*T(i,j);
As @VBBV stated, you have an extra ) in your code. If you're editing this in the MATLAB Editor, Code Analyzer should have flagged this line. Indeed, it flags the second ) after T(i,j+1) as well as the other ) following it on the line.
end
As for the other message you listed, check that you haven't created a script file ode15s.m. If you have, rename it. The following command should only list files under matlabroot.
which -all ode15s
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!