MATLAB Online does not run script properly

I ran the following script (which I named draft.m) through MATLAB Online:
clear;
fun = @(x, b) (b-7)*(b+5)*x.^2-2*x+3;
brange = -8:0.1:10;
fmin = [];
for b = brange
end
and got the following error message:
>> draft
Error: File: draft.m Line: 6 Column: 1
At least one END is missing. The statement beginning here does not have a matching end.
But if I remove the line
fmin = [];
then it works. Did I miss something? I suppose defining an empty array shouldn't fail the for loop, should it? Thank you very much in advance for any comments that you may have.

3 commentaires

Martin
Martin le 10 Avr 2024
Déplacé(e) : Dyuman Joshi le 10 Avr 2024
How to convert a matrix from real numbers into a Modulo, for example, how would I convert the following matrix into modulo 7?
M = [7 3 7; 1 0 9; 7 2 8]
Dyuman Joshi
Dyuman Joshi le 10 Avr 2024
@Martin, Have you tried searching on the internet or on this forum for the same?
Cris LaPierre
Cris LaPierre le 10 Avr 2024
This is unrelated to this question. Please ask this in a new question.

Connectez-vous pour commenter.

Réponses (1)

Code runs without error for me. I can also run it here without error. Perhaps try again? If it happens again, perhaps there are more details you can share about what you are doing?
clear;
fun = @(x, b) (b-7)*(b+5)*x.^2-2*x+3;
brange = -8:0.1:10;
fmin = [];
for b = brange
end
b
b = 10

8 commentaires

The following is the complete code:
fun = @(x,b) (b - 7)*(b + 5)*x.^2 - 2*x + 3;
brange = -8:.01:10;
fmin = [];
for b = brange
f = @(x) fun(x, b);
[crit, fval] = fminbnd(f, -6, 6);
fmin(end + 1) = fval;
end
plot(brange, fmin);
xlabel('b'); ylabel('m(b)'); title('The graph of m(b)');
I am plotting the minima of the parametrized functions (given by fun and f in the code) versus b. I am pretty sure it should work -- I ran the code before and I also tried it on GNU Octave just now. Here's the plot from the latter:
I am really not sure what's happening with my MATLAB Online and hence I posted the question.
Any clues?
Thanks.
By the way, if it helps, I am using MATLAB Online through Firefox 100.0 (64-bit) on macOS Big Sur.
Thanks again.
Torsten
Torsten le 9 Mai 2022
And the error message is the one you posted above:
Error: File: draft.m Line: 6 Column: 1
At least one END is missing. The statement beginning here does not have a matching end.
?
Yes, in both cases -- with the incomplete and the complete codes. And here's the content of my MATLAB drive:
>> ls
calc_roots.m draft.m factorial2.m funxy.m matlab_logo.jpg prob2.m square.m
dist2.m Euler fig3_4_2.png g.m my_dfact.m Published stats_2.m
draft.asv Exam friendship_paradox.mlx Martinez prob1.m Shared test_dist2.m
whch I don't see any conflicts with the script -- or maybe I missed something?
Thank you very much.
Torsten
Torsten le 9 Mai 2022
Modifié(e) : Torsten le 9 Mai 2022
Just a guess:
What if you use
fmin = [fmin,fval];
instead of
fmin(end+1) = fval;
or even better:
fmin = zeros(numel(brange),1)
for b = brange
f = @(x) fun(x, b);
[crit, fval] = fminbnd(f, -6, 6);
fmin(b) = fval;
end
It ended up with the same error message. In fact, in the incomplete version of the code, neither of the statements is within the loop and code still failed to work. And, I assume
fmin(end+1) = fval;
is syntatically correct?
Confusing indeed. Once in a while having fun with bizzarre behavior of our machines. Hehe.
The full code runs fine for me.
This helps! Thanks a lot!
Take care.

Connectez-vous pour commenter.

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by