matlab invalid expression error
Ältere Kommentare anzeigen
How do I correct this? I'm writing code for a 2nd order bvp, and have ann invalid expression error on this line.
function dydx = odefun( z, y, wc, (pi*D^2)/4*k, qs, {piDh)*(T-Ta) )
dydx = [ y(2);
( wc * y(2) - qs * y(1) - (piDh)*(T-Ta) ) / piD^2/4*k ];
end
I've tried adjusting the parentheses, not really sure what next to do.
4 Kommentare
Walter Roberson
am 12 Feb. 2021
When you use that syntax, what do you expect MATLAB to do with that 4th parameter, (pi*D^2)/4*k ? And is your expectation that MATLAB will consider that an exact match for piD^2/4*k even though the () are different ?
izuchukwu morah
am 12 Feb. 2021
Walter Roberson
am 12 Feb. 2021
If I understand correctly, your expectation for
function dydx = odefun( z, y, wc, (pi*D^2)/4*k, qs, (piDh)*(T-Ta) )
is that anywhere in the code that (pi*D^2)/4*k occurs, you would want that expression replaced by the value passed in as the fourth parameter, and that anywhere in the code that (piDh)*(T-Ta) occurs, you would want that replaced by the value passed in as the sixth parameter?
What would your expectation be if the code contains
temp = (pi*D^2)/2*k
which, mathematically, would be twice the (pi*D^2)/4*k expression ? Would you expect it to be replaced with twice the value that was passed in as the fourth parameter? If the code contained (pi*D^2)/4 which is k times the value passed in as the fourth parameter, then what would you expect MATLAB to use as the value?
Your code does not currently contain (pi*D^2)/4*k and instead contains piD^2/4*k which is different not only in the () but in the fact that it contains a variable piD whereas the expression in the header was pi*D
If, hypothetically, you had had
function dydx = odefun( z, y, wc, (pi*D^2)/4*k, qs, (piDh)*(T-Ta), x^2+y^2 )
then is your expectation that MATLAB should be able to deduce the value of x^2-y^2 if that appeared in the code? Since, theoretically, it could see that it had x^2+y^2 and also had y, and so could square the y passed in and subtract twice that from the x^2+y^2 passed in in order to get the x^2-y^2 ? And if the expression in the code were x by itself, then hypothetically it could take the passed in x^2+y^2 and subtract the square of the passed in y, leaving x^2 which it could take the square root of... well, except for the fact that it would have lost the sign of x along the way...
izuchukwu morah
am 12 Feb. 2021
Akzeptierte Antwort
Weitere Antworten (2)
Walter Roberson
am 12 Feb. 2021
MATLAB does not work according to your expectations. No computer languages reason about the relationships between variables automatically to deduce values not stated. With the possible exception of SNOBOL or similar languages.
function dydx = odefun( z, y, wc, piD24k, qs, piDhTTa )
%piD24k is pi*D^2/4*k
%piDhTTa is pi*Dh*(T-Ta)
dydx = [ y(2);
( wc * y(2) - qs * y(1) - piDhTTa ) / piD24k ];
end
See, give full names for the expressions and use the same names where you want the values to be substituted. No deduction needs to be done to find the values of the variables. Use better variable names if you want.
4 Kommentare
izuchukwu morah
am 12 Feb. 2021
Steven Lord
am 12 Feb. 2021
These are the only places in your code where Tanalytic appears.
% Tanalytic = T0 + (Ttarget - T0) * ( ( exp( wc*z / (pid24k) ) - 1 )/( exp( wc*L / (pid24k) ) - 1 ) ) ...
% + ( (piDhTTa) )*L / (piD24k) ) * ( ( z/L - exp( wc*z / (piD24k) ) - 1 )/( exp( wc*L / (piD24k) ) - 1 ) ) ;
%piD24k is pi*D^2/4*k
%piDhTTa is pi*Dh*(T-Ta)
% plots:
figure;
plot( x, T, '-b', x, Tanalytic, '+r');
You never compute the value of the variable Tanalytic (the line of code that would do so is commented out) so that variable doesn't exist on the line where you try to plot it.
Walter Roberson
am 12 Feb. 2021
Your code never assigns to that variable, except in some comments.
izuchukwu morah
am 13 Feb. 2021
Steven Lord
am 12 Feb. 2021
When you define your function, the function declaration line should include the names of the variables into which the input arguments will be stored. It cannot contain an expression that is not a name.
When you call your function, specify the exact values on which you want your function to operate.
This is an incorrect way to define an addme function:
function z = addme(2, 3)
z = 2+3;
end
This is a correct way to define the addme function:
function z = addme(x, y)
z = x + y;
end
and this is a correct way to call the addme function.
theOutput = addme(2, 3)
Kategorien
Mehr zu Boundary Value Problems finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

