Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I transfer this m-file to make it into a Function m-file in MATLAB? This

ID: 2998523 • Letter: H

Question

How do I transfer this m-file to make it into a Function m-file in MATLAB? This is the only thing i have left before i turn in my project, if you can help me clear this up i would appreciate it.

clear all;
a=input('Enter any desired row vector in []:');
n=length(a);
for ii=1:n
fprintf('ii = %d ', ii);
for jj = 1:n
fprintf(' jj = %d ', jj);
for kk=1:n
fprintf(' kk = %d ', kk);
A(ii,jj)= a(ii)*a(jj)*a(kk);
end
end
end
A
plot(A)
  

I keep getting these errors:

Line 1: Invalid syntax at 'Enter any desired row vector in []:'. Possibly, a ), } , or ] is missing.

Line 1: Parse error at ')' : usage might be invalid MATLAB syntax

! Line 1: The function return value 'a' might be unset.

Explanation / Answer

ANSWER:

To convert a piece of code into a function to which parameters can be passed the folowing is the syntax to be used.

function func_name(variable) where func_name is the name of the function we intend to keep and variable is the variable to be passed to the function.

function assignment(a)

clear all;
n=length(a);
for ii=1:n
fprintf('ii = %d ', ii);
for jj = 1:n
fprintf(' jj = %d ', jj);
for kk=1:n
fprintf(' kk = %d ', kk);
A(ii,jj)= a(ii)*a(jj)*a(kk);
end
end
end
plot(A);

The above change has to be incorporated.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote