2 views (last 30 days)

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

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.

John D'Errico
on 24 Nov 2020

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.

Steven Lord
on 24 Nov 2020

Steven Lord
on 23 Nov 2020

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]

S1 = A^2

S2 = A.^2

If your matrix is not square (for example, if it is a row vector) then .^ will work but ^ will not.

thisWillWork = (1:5).^2

thisWillNOTWork = (1:5)^2

Steven Lord
on 23 Nov 2020

Image Analyst
on 24 Nov 2020

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

Start Hunting!
## 0 Comments

Sign in to comment.