I need help with the following matlab question so far I have

I need help with the following matlab question.

so far I have the following code. I need help displaying an error message when the matrix deminsions are wrong for multiplication and division.

str=input(\'Choose opperation, type(add, sub, mul, or div) : \' ,\'s\');

x=input(\'Enter scalar, vector, or matrix variable for x : \');

y=input(\'Enter scalar, vector, or matrix variable for y : \');

add_num= strcmp(\'add\',str);

sub_num= strcmp(\'sub\',str);

mul_num= strcmp(\'mul\',str);

div_num= strcmp(\'div\',str);

if add_num==1

[rowy, coly] = size(y);

[rowx, colx] = size(x);

if rowy==rowx && coly==colx

r=x+y;

disp(\'f(x,y) = \')

fprintf([repmat(\'%4.2f\\t\', 1, size(r, 2)) \'\ \'], r\')

else

disp(\'ERROR: Matrix Dimensions\')

end

else if sub_num==1

[rowy, coly] = size(y);

[rowx, colx] = size(x);

if rowy==rowx && coly==colx

r=x-y;

disp(\'f(x,y) = \')

fprintf([repmat(\'%4.2f\\t\', 1, size(r, 2)) \'\ \'], r\')

else

disp(\'ERROR: Matrix Dimensions\')

end

else if mul_num==1

[rowy, coly] = size(y);

[rowx, colx] = size(x);

if rowy==rowx && coly==colx

r=x.*y;

disp(\'f(x,y) = \')

fprintf([repmat(\'%4.2f\\t\', 1, size(r, 2)) \'\ \'], r\')

else

disp(\'ERROR: Matrix Dimensions\')

end

else if div_num==1

[rowy, coly] = size(y);

[rowx, colx] = size(x);

if rowy==rowx && coly==colx

r=x./y;

disp(\'f(x,y) = \')

fprintf([repmat(\'%4.2f\\t\', 1, size(r, 2)) \'\ \'], r\')

else

disp(\'ERROR: Divison not available for matricies\')

end

end

end

end

end

Consider the case where the input to a program can be either a scalar, vector, a matrix or a combination of these. Write a program that can add, subtract, multiply or divide the input variables. Note that the division portion is not valid for matrices (we use inverses instead) – you will need to incorporate this in your code. Your program should prompt the user for the type of operation to be performed, to enter the two variables and display the results neatly (using fprintfor the appropriate error message if the code fails to execute. Test your code using at least 7 different combinations of x and y, allowing each to be scalars, matrices of various shapes (not only square) and vectors.

Solution

Can you be bit more specific in the comments ? Because it seems to be displaying error messages in mul and div too

I am adding sample runs:

Choose opperation, type(add, sub, mul, or div) : mul
Enter scalar, vector, or matrix variable for x : [1 2 ]
Enter scalar, vector, or matrix variable for y : [1 2 3]
ERROR: Matrix Dimensions


Choose opperation, type(add, sub, mul, or div) : div
Enter scalar, vector, or matrix variable for x : [1 2 ]
Enter scalar, vector, or matrix variable for y : [2 4]
f(x,y) =
0.50   0.50  


Choose opperation, type(add, sub, mul, or div) : div
Enter scalar, vector, or matrix variable for x : [1 2 3]
Enter scalar, vector, or matrix variable for y : [1 2]
ERROR: Divison not available for matricies

Choose opperation, type(add, sub, mul, or div) : div
Enter scalar, vector, or matrix variable for x : [1 2; 3 4]
Enter scalar, vector, or matrix variable for y : [1 3 1; 1 3 1];
ERROR: Divison not available for matricies

I need help with the following matlab question. so far I have the following code. I need help displaying an error message when the matrix deminsions are wrong f
I need help with the following matlab question. so far I have the following code. I need help displaying an error message when the matrix deminsions are wrong f
I need help with the following matlab question. so far I have the following code. I need help displaying an error message when the matrix deminsions are wrong f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site