Write a function in Matlab that would take as input a row matrix.

1 view (last 30 days)
* Use a for loop to compute the square of each element in A and store the square values in matrix B.
function [B] = Square(A)
* Recall the function file that you created from the command window to take an input A=1:10
* Use plot function to plot B versus A
Can anyone help me understand the question so i can try solving it?
will i be using arrays in this problem? and how will i plot a graph?

Accepted Answer

Image Analyst
Image Analyst on 23 Nov 2013
You can do this all in one m-file. Have an m-file called test_square.m and in there have both the test harness (calling function) and the Square function. It will look similar to this:
function test_square()
A = 1:10;
B = Square(A);
plot(A, B, 'ro-');
grid on;
function B = Square(A)
for k = 1 : length(A)
B.......
.... and so on.
In the loop you square each element of A because this is exactly what it told you to do. Then just click the green triangle, or type test_square on the command line, to run the program.
  3 Comments
Image Analyst
Image Analyst on 24 Nov 2013
r means red. o means to put a little circle at the data point. - means to connect the data points with a solid line between them.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 23 Nov 2013
Hint:
for K = 1 : 20
B(K) = A(K) + 5; %for example
end

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!