Why does my matrix give me a syntax error

%first created function to make an equation for all the calculations
function Fn = Fnorm(A)
%the matrix's space is now set
[m,n] = size(A);
%set sum = to 0 first to allow for a fresh start
sum = 0;
%for the amount of spaces in the matrix,
% the forbenius norm will account for the sum
for i=1:m
for j=1:n
sum = sum + (A(i,j)*A(i,j));
end
end
Fn = sqrt(sum);
%matrix A is defined
A = [5 7 9; 1 8 4; 7 6 2];
fprintf ('The Forbenius Norm of the Matrix A is:\n')
end

4 Comments

Why does my matrix give me a syntax error
The problem with not copy/pasting your error messages into your posts is that someone else running your code may see something quite different and not know what you're talking about:
Fnorm(eye(3))
The Forbenius Norm of the Matrix A is:
ans = 1.7321
Error: File: Fnorm.m Line: 21 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "Fnorm".)
this is the error message
What did you pass in for A?
Show us the exact line of code where you called the function.
You didn't just press the green run triangle without passing in A did you?
Why do you pass in A but then set it later with this line
A = [5 7 9; 1 8 4; 7 6 2];
but then never use that A? Don't overwrite the A that the user passes in with some A from inside the function.
Your problem is clearly you don't know how to spell Frobenius. :)
https://mathworld.wolfram.com/FrobeniusNorm.html#:~:text=The%20Frobenius%20norm%2C%20sometimes%20also%20called%20the%20Euclidean,can%20also%20be%20considered%20as%20a%20vector%20norm.

Sign in to comment.

Answers (2)

Note that [A] is not to be defined within Fnorm that should be called correctly. It works without any err, if you call your fcn with this:
A = [5 7 9; 1 8 4; 7 6 2];
Fn =Fnorm(A)
The Forbenius Norm of the Matrix A is:
Fn = 18.0278
function Fn = Fnorm(A)
%the matrix's space is now set
[m,n] = size(A);
%set sum = to 0 first to allow for a fresh start
sum = 0;
%for the amount of spaces in the matrix,
% the forbenius norm will account for the sum
for i=1:m
for j=1:n
sum = sum + (A(i,j)*A(i,j));
end
end
Fn = sqrt(sum);
% matrix A is provided while callin this fcn: Fnorm(A)
% No need to defined [A] here!
fprintf ('The Forbenius Norm of the Matrix A is:\n')
end

1 Comment

@Eric Hofmann, in other words, you need
A = [5 7 9; 1 8 4; 7 6 2];
Fn =Fnorm(A)
function Fn = Fnorm(A)
%code
end
NOT
function Fn = Fnorm(A)
%code
end
A = [5 7 9; 1 8 4; 7 6 2];
Fn =Fnorm(A)
because the script must come first in a file, then the function definitions. You can't have function definitions first, then the script after them.
Attach your actual m-file with the paperclip icon if you still need help.

Sign in to comment.

Matt J
Matt J on 12 Sep 2021
(It follows the END that terminates the definition of the function "Fnorm".)
That is not allowed. All local functions must be placed at the end of the mfile and any script commands not contained in a function must be at the beginning.

Categories

Find more on Functions in Help Center and File Exchange

Asked:

on 11 Sep 2021

Edited:

on 12 Sep 2021

Community Treasure Hunt

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

Start Hunting!