how do I right x^2 in MATLAB

i trying to do some equetion and i come up with an erro message every time i right it out

 Accepted Answer

John D'Errico
John D'Errico on 23 Nov 2020
Edited: John D'Errico on 23 Nov 2020
Is x a scalar? If so, then you write it as x^2.
Is x a vector? Then, assuming you want to perform an element-wise squaring, you use x.^2.
If you are asking this question, you REALLY need to be reading the getting started tutorials - the Onramp tutorials are a good place to start. There are also multiple MATLAB tutoring sessions on Youtube to watch.
Finally, when you get an error. then show what you did. Show the COMPLETE error message, thus everything in red. Otherwise we need to make wild guesses as to what you did wrong.

14 Comments

it says this Error using ^
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 '.^'.
i have x= to servall values
and f(x)=x^2 exp(- x/3)
Doing Onramp sounds like a good suggestion for. It really is a few hours well spent. You will be guided through the basics of Matlab. It should teach you why you are provided with the solution you read here.
As Rik says, you really need to read the tutorials. They help you to get started. How else will you learn?
As I said, if the variable x is not a scalar, then you need to be careful when using ^ to square the elements of x. It won't do as you want. In fact, it will cause an error. The error willl be the one you got.
I did tell you which operator to use.
Well i understand the basic of matlab but when i get x=0:0.1:1.9 the need to have f(x)= x^2 exp (-x/3) it does not work... i have tryed both methods of hwo to whright squered but nothing workers...
No. Apparently you do not understand the basics of MATLAB. SHOW WHAT YOU TRIED, if you want help.
I can assure you that it does work, IF you use the proper operators. Note that in order to multiply two vectors, again, element-wise, you need to use the .* operator. Of course, you would have learned this, had you tried reading or watching the many sources of tutorials that are available. It is ok to read the manual.
So, how did you write that relation?
x = 0:0.1:1.9;
f = x.^2 % (now, you need to add the rest of this line.)
i have tryed that it didnt work so explain that........
Sigh. You keep on saying "it didn't work." SHOW WHAT YOU TRIED. SHOW WHAT ERROR YOU GOT.
Are you trying to tell me that this does not square the elements of x?
x = 0:0.1:1.9;
f = x.^2
f = 1×20
0 0.0100 0.0400 0.0900 0.1600 0.2500 0.3600 0.4900 0.6400 0.8100 1.0000 1.2100 1.4400 1.6900 1.9600 2.2500 2.5600 2.8900 3.2400 3.6100
Or, if you want to create a function,
f_x = @(x) x.*2;
f(1:3)
ans = 1×3
0 0.0100 0.0400
In both cases, they work quite nicely. Now were you listning when I explained how to multiply vectors? What operator do you use to multiply the elements of both vectors? For example, you might write x.^2 in another way, using x.*x. This would effectively square every element in the vector x.
What if you wanted to multiply x with exp(x), where x is a vector? What operator would you use? How does that impact what you want to do?
That is, think of it like this. You want to form x^2*exp(x/3). But x is a vector. So we already know how to square the elements of a vector.
x.^2
How about exp(x/3)? You can always divide a vectror by a scalar, thus x/3. The result will be a vector, right?
Can you use exp on a vector? Yes. So is exp(x/3) a vector? Yes.
So now, you should have learned how to multiple a vector (x.^2) with the elements of the other vector exp(x/3)? What do you write?
so if was trying to do x^exp(- x/3) how would you right it?
What do you think? Look in the second code section in his comment. Why are you ignoring everything he says?
And why do you refuse to use the documentation or any of the tutorials and videos you may find online?
thanks this is called learing, not soing where i look on youtube and then get confuesed and still not understand it i rath have some go throught it with me on to on...
Youu may not agree on that but that os how i learn.......
I dont i think it also usfall but when i have a quetion so persicfic for matlab then it wont help....
Its like the maths stuff i have to do there is tutrials to go throught but nothing for the question you need so I then get confued and lose hope
Honestly, I think you are giving up, not trying. It is just too overwhelming for you, so you give up before you ever start. Then you just ask someone to write the code for your assignments. You won't learn that way. So try this:
Given two vectors, x and y, can you multiply the elements of the two vectors?
x = [1 2 3 4];
y = [2 3 5 7];
The result (element-wise product) should be
z = [2 6 15 28]
What one line of code would have you multiply the vectors x and y? I've already told you how to do it. So try it. Show me that you want to learn. And then we can take the next step, building up what you need to do to get a solution.
John and Rik both suggested that you work through the MATLAB Onramp tutorial and I agree with that suggestion. It is about a two hour long free course with videos and hands-on exercises that are automatically scored (so you get feedback about whether your answer is correct right away) and is designed to teach you the basics of working with MATLAB. I suspect after taking sections 4 and 6 you will understand the difference between the ^ and .^ operators and be able to answer your question.

Sign in to comment.

More Answers (1)

To raise a square matrix to a power use the ^ operator.
To raise each element of an array (the array need not be square or even 2-dimensional) to a power use the .^ operator.
A = [1 2; 3 4]
A = 2×2
1 2 3 4
S1 = A^2
S1 = 2×2
7 10 15 22
S2 = A.^2
S2 = 2×2
1 4 9 16
If your matrix is not square (for example, if it is a row vector) then .^ will work but ^ will not.
thisWillWork = (1:5).^2
thisWillWork = 1×5
1 4 9 16 25
thisWillNOTWork = (1:5)^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 '.^'.

4 Comments

could i possibel ask you more question ....?
If you're asking if you can email me directly, I'm on vacation for the next week or so (only sporadically checking Answers.) Posting your follow-up questions as comments on this post (or as new posts, if they're unrelated to this topic) will let others answer more quickly than I will.
you seam more helpfull... my e male is alex-walton3@sky.com
Alex, why won't you ask questions here so all of can benefit from the discussion (like Steven specifically said)? Most volunteers here don't like offering free, private consulting via email when the Mathworks went to all this trouble to set up this nice forum for everyone's benefit. Besides, other people are helpful too. We volunteers range anywhere from people who simply give you the answer outright, to those like John who try to guide you to the answer so that you learn it more deeply than just being handed an answer. Even though John and Steve's approach may not be the fastest today, if you learn MATLAB, you will be faster in the future and won't have to ask. Indeed, hopefully you'll be the expert one day and be helping others here.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!