From: Mario Fatafehi on
I have this function but when I called like [y z] = lab2_15192572_fun(-0.8) it gives me repeated answer and I am not sure whether it is the right answer or not. The answer is z = 1.6400 y = -0.3600 y = -0.3600 z = 1.6400 Is my function structure wrong?

function [y,z] = lab2_15192572_fun(x)
if abs(x) < 2
z = x^2 + 1
y = x^2 - 1
elseif 2< abs(x) <= 4
z = 1/(x^2 + 1)
y = 1/(x^2 - 1)
else
z = cos(x)
y = sin(x)
end
From: Walter Roberson on
Mario Fatafehi wrote:
elseif 2< abs(x) <= 4

2 < abs(x) will return either 0 (false) or 1 (true). You then check to
see if that 0 or 1 is <= 4, which of course it is.

If you want to check to see if a number is in a range, code both halves
of the test, 2 < abs(x) && abs(x) <= 4
From: Mario Fatafehi on
Walter Roberson <roberson(a)hushmail.com> wrote in message <hoeurm$nga$1(a)canopus.cc.umanitoba.ca>...
> Mario Fatafehi wrote:
> elseif 2< abs(x) <= 4
>
> 2 < abs(x) will return either 0 (false) or 1 (true). You then check to
> see if that 0 or 1 is <= 4, which of course it is.
>
> If you want to check to see if a number is in a range, code both halves
> of the test, 2 < abs(x) && abs(x) <= 4

But why does it gives me repeated answer?
From: Wayne King on
"Mario Fatafehi" <coruba9(a)hotmail.com> wrote in message <hof3hk$pes$1(a)fred.mathworks.com>...
> Walter Roberson <roberson(a)hushmail.com> wrote in message <hoeurm$nga$1(a)canopus.cc.umanitoba.ca>...
> > Mario Fatafehi wrote:
> > elseif 2< abs(x) <= 4
> >
> > 2 < abs(x) will return either 0 (false) or 1 (true). You then check to
> > see if that 0 or 1 is <= 4, which of course it is.
> >
> > If you want to check to see if a number is in a range, code both halves
> > of the test, 2 < abs(x) && abs(x) <= 4
>
> But why does it gives me repeated answer?

Mario, why do you say it gives repeated answers? This code

function [y,z] = lab2_15192572_fun(x)
if abs(x) < 2
z = x^2 + 1;
y = x^2 - 1;
elseif 2< abs(x) <= 4
z = 1/(x^2 + 1);
y = 1/(x^2 - 1);
else
z = cos(x);
y = sin(x);
end

only returns z=(0.8^2+1) and y=(0.8^2-1) when you input (-0.8)

Wayne
From: Jan Simon on
Dear Mario!

> I have this function but when I called like [y z] = lab2_15192572_fun(-0.8) it gives me repeated answer and I am not sure whether it is the right answer or not. The answer is z = 1.6400 y = -0.3600 y = -0.3600 z = 1.6400 Is my function structure wrong?

> function [y,z] = lab2_15192572_fun(x)
> if abs(x) < 2
> z = x^2 + 1
> y = x^2 - 1
> ...

Do you mean, that the answer appears repeatedly in the command window? This is caused by the missing semicolon after the expression. Try this instead:
z = x^2 + 1;
y = x^2 - 1;

Kind regards, Jan
 |  Next  |  Last
Pages: 1 2
Prev: jpeg encodercode in matlab
Next: reshape