MATLAB Answers

0

i am trying to write a function.

Asked by rihan jericho on 30 Jan 2018 at 13:02
Latest activity Edited by Jan Simon
on 31 Jan 2018 at 16:07

for example i want to calculate the area of a triangle. so i will use the formula 1/2(b*h). and i also want to calculate volume of a triangle and i will use 1/2(b*h*l). so when i am passing the base(b) and the height(h), i want it to give me the area, and when i am passing the base(b), height(h) and length(l) i want it to give me the volume. i want to use function for my input arguments. How can i do it?

  5 Comments

When you delete your question, you insult the person who spent the time to try to help you. You make their answer useless to anyone else.

An by posting homework questions, and then deleting them you are in effect asking someone to help you to cheat.

And this is why I will never answer another of your questions again.

Why do we care that you have finished your homework? Answers is here to help others too. They might have been able to learn from your question.

You never should have posted the question in the first place if you were attempting to cheat on your homework. And if your teacher would have been unhappy at seeing the question, then you were indeed cheating.

And, yes, I do remember that you said your teacher would kill you if they saw your question posted here. Deleting the comment does not delete it from my memory.

Personally, I do hope that your teacher happens to see this question, and then starts asking questions of you as to what the question was. Anyway, they can surely recognize what you asked, merely by reading the answers. So my fervent hope is that you will sill be unmasked as a person who is quite willing to cheat on their work. Sorry, but eventually your actions will catch up to you unless you start to learn personal responsibility.

As I said before, all you do is ensure that we will avoid answering further questions by you, or close them when we recognize them for what they are.

Jan Simon
on 31 Jan 2018 at 16:05

@rihan: Your question can be found in Google's cache, even if you delete it here:

 for example i want to calculate the area of a triangle. so i will
 use the formula 1/2(b*h). and i also want to calculate volume of
 a triangle and i will use 1/2(b*h*l). so when i am passing the
 base(b) and the height(h), i want it to give me the area, and
 when i am passing the base(b), height(h) and length(l) i want it
 to give me the volume. i want to use function for my input
 arguments. How can i do it?

It is no cheating, if you ask for hints and not for a solution. Why should your teachers care as long as you do not try submit a solution written by someone else? Homework questions are welcome in this forum, as long as they are explicitly marked as homework and contain a specific question. Posting the current code and asking for assistance is not cheating. See https://www.mathworks.com/matlabcentral/answers/8626-how-do-i-get-help-on-homework-questions-on-matlab-answers.

But you have deleted the contents of several of your questions now. This is not a fair usage of the forum. John is right: The members of the forum will avoid answering. And they are smart enough to recognize, that you change your account name repeatedly.

Log in to comment.

Tags

2 Answers

Answer by Jan Simon
on 31 Jan 2018 at 10:22
 Accepted Answer

A small example code:

function r = Calculate(a, b, c)
switch nargin
  case 2
    disp(a)
    disp(b)
    r = a * b;
  case 3
    disp(a)
    disp(b)
    disp(c)
    r = a * b + c;
  otherwise
    error('2 or 2 inputs required');
end

If you do not know how many inputs the function will get, use varargin. See

doc nargin
doc varargin

  0 Comments

Log in to comment.


Answer by John D'Errico
on 30 Jan 2018 at 13:06
Edited by John D'Errico
on 30 Jan 2018 at 13:08

Learn to use nargin, so you can test to see how many arguments were provided. Then you can put a branch in to do the calculation as you want.

That is, you can write a function to have three arguments, but if only two arguments were provided when you call it, MATLAB assumes it is the first two arguments. So nargin allows you to differentiate in how the function was called.

  0 Comments

Log in to comment.


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today