functions at end of file - THEY ARE

So, I"m expanding my horizons a bit and messing with functions, I need to use the quad command... anyhoo...
trying to get a function set up (apparently the quad comands needs one as input?) and I keep getting the "functions must be at the end of the file." The problem is, THEY ARE - shown here is the code I've gotten together so far. I've even tried getting rid of one function for troubleshooting, just to see if it wasn't happy with the formatting. Doesn't seem happy with anything I do here!
dff

1 Comment

Stephen23
Stephen23 on 13 Sep 2022
Edited: Stephen23 on 13 Sep 2022
"The problem is, THEY ARE "
This neatly illustrates why it is always useful/important to pay attention to the syntax highlighting: if you had checked all of the underlined code, then you would have resolved this issue very quickly by yourself:

Sign in to comment.

 Accepted Answer

semi-colon is not part of the end syntax. Your code is the same as if you had written
function [f] = load(x,C1,C2)
[f] = sqrt(C1*x+((C2*(x^2))^0.5));
end
;
function [fx] = quant(x,C1,C2)
[fx] = x*sqrt(C1*x+((C2+(x^2))^0.5));
end
;
The two semi-colons are outside any function.
Side note: I recommend that you be consistent about whether you use sqrt() or you use ^0.5 -- otherwise you leave readers asking what the difference is between those that you would want to do the operations differently.
Is there a difference? Yes, there is for non-scalar values. sqrt() is element-wise, but ^0.5 is matrix-power -- x^2 is x*x where x is "inner product" rather than being element-by-element multiplication.
So the careful reader will start to wonder whether you are intentionally expecting square matrices that are non-scalar, that you want to be using expression^0.5 differently than sqrt(expression)

2 Comments

Thank you! Also for the tidbit about the sqrt(), I actually didn't know that there was a difference!
You should also probably not name your function load, as that will shadow the internal function that loads mat files.

Sign in to comment.

More Answers (0)

Categories

Find more on Files and Folders in Help Center and File Exchange

Products

Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!