Please check if I have wrote the functions correctly:

2 Ansichten (letzte 30 Tage)
Konrad Krawczyk
Konrad Krawczyk am 21 Mai 2020
Bearbeitet: John D'Errico am 21 Mai 2020
Please check if I have wrote the functions correctly:
1) f(x) = lnx / x ==> @(x) log(x) / x
2) f(x) = 1/2 * (ln2)^2 ==> @(x) 1/2*(log(2))^2
3) f(x) = ln(x+0,001) ==> @(x) log(x+0,001)
4) f(x) = (1001 * ln1001)/1000 - 3ln10-1 ==> @(x) (1001 * log(1001))/1000 – 3*log(10)-1
5) f(x) = x * sin30x * cosx ==> @(x) x*sin(x.30) * cos(x)
6) f(x) = - (90 * pi) / 899 ==> @(x) -((90*pi)/899)

Akzeptierte Antwort

John D'Errico
John D'Errico am 21 Mai 2020
Bearbeitet: John D'Errico am 21 Mai 2020
There are three issues that you have problems on here, although only one of them may be perceived as significant.
The important problem is in #5. You need to appreciate that while we write the product 30x as that, with only an implied multiplication operator present, MATLAB needs one. As importantly, that operator is NOT a dot, it is the * operator. I think you know that, but as you learn to write code, you will need to do so more carefully. Develop the habit of using operators.
Next, your functions are not vectorized. So, if you write the function:
f = @(x) log(x)/x;
And then try to use it in MATLAB
f(1:3)
ans =
0.3344
it returns a number. But the number is not what you would have expected, and you would have expected 3 numbers. In fact, here MATLAB is using linear algebra to do a calculation that you did not expect, and that is because you were using the wrong operator.
Perhaps you have not yet learned about the .* and ./ and .^ operators. They allow you to write expressinos in MATLAB that will operate on entire vectors of arrays of numbers.
If you wish to square each element of a vector, then you use .^, not the ^ operator.
x = 1:3;
x^2
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform
elementwise matrix powers, use '.^'.
x.^2
ans =
1 4 9
As you can see, when I square each element of the vector, I get the expected/desired result. If I use ^ I get n error message.
>> x*x
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of
rows in the second matrix. To perform elementwise multiplication, use '.*'.
>> x.*x
ans =
1 4 9
>> x/x
ans =
1
>> x./x
ans =
1 1 1
As you can see, the same thing applies for multiplication. In order to get the desired answer, operating on each element, one at a time, you need to use .* and ./ to get what you would have expected.
The distinction is important. There are cases where you do not need to worry. MATLAB is able to multiply a vector with a scalar.
>> 2*x
ans =
2 4 6
>> x*2
ans =
2 4 6
It can also properly divide a vector by a scalar. However, dividing a scalar by a vector causes a problem.
>> x/2
ans =
0.5000 1.0000 1.5000
>> 2/x
Error using /
Matrix dimensions must agree.
>> 2./x
ans =
2.0000 1.0000 0.6667
So until you get to the point where you know the rules reflexively, it will be safest if you get used to the habit of using the dotted operators when working with vectors and arrays.
I would also add that addition and subtraction are always properly vectorized in MATLAB. There is no .+ or .- operator needed, nor do they even exist. You can always add or subtract any two vectors or arrays as long as the sizes conform, and MATLAB is smart enough to perform the expansion of scalars to add or subtract a scalar constant with vectors or arrays.
Finally, a constant is not a vectorized function in MATLAB.
That is, if I want to write the function f(x)=17, I could do as you have done. In fact, it does work, in a fashion.
>> f = @(x) 17
f =
function_handle with value:
@(x)17
>> f(3)
ans =
17
>> f(1:3)
ans =
17
So when passed a scalar value, it returns the scalar value 17. However, when I tried the same with a vector or array input, it fails, just returning the SCALAR value 17 as you see. Instead, try one of these approaches:
f = @(x) 17.*ones(size(x));
or...
f = @(x) repmat(17,size(x));
f(1:3)
ans =
17 17 17
I would prefer the latter form, since the former, while it works, requires MATLAB to multiply every element of a vector or array by a number. As such, it would consume CPU cyles unnecessarily.
Anyway, the only real "problem" in your solutions was in case #5. The rest of the issues I mention will only apply when you start to work with vectors or arrays of numbers.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by