1

I have a formula

F = (-k.^(3/2) .* sqrt(4 .* c .* x + k) + 2 .* x .* k .* c + k.^2) / (2 .* c)

and i'm trying to plot F against a range of values of c for a constant x and k value like so:

x = 0.01;
c = 10000:10000:100000;
k = 100000;
F = (-k.^(3/2) .* sqrt(4 .* c .* x + k) + 2 .* x .* k .* c + k.^2) / (2 .* c)

At this point I assumed matlab would give me a vector with the same size as c but it just prints:

F =

   47.1563

Plotting F against a range of k values for constant c and x works fine, but the above does not.

Can anybody explain this for me?

JavascriptLoser
  • 1,853
  • 5
  • 34
  • 61
  • The division. you are using matrix division, you forgot that small point you put in themultiplocations in the division!! / -> ./ – Ander Biguri Sep 29 '14 at 10:39
  • I am guessing right now but i think you are changing a `vec(a)=b*vec(c)` into `b=vec(a)/vec(c)` so you get a scalar value. Try to do `(...).*(2.*C).^(-1)` – The Minion Sep 29 '14 at 10:40

1 Answers1

6

Use element wise division ./, similar to .*

F = (-k.^(3/2) .* sqrt(4 .* c .* x + k) + 2 .* x .* k .* c + k.^2) ./ (2 .* c)

The / is used for right-matrix division: A/B is equivalent to mrdivide(A,B),which solves the system of (linear) equations x*B = A for x, whereas ./ is just element wise division.

MeMyselfAndI
  • 1,320
  • 7
  • 12
  • 1
    +1 but you should add an explanation of why `/` gives a single value back – Dan Sep 29 '14 at 10:39
  • Good point. `/` is right-matrix division. `A/B` is equivalent to `mrdivide(A,B)`,which solves the system of (linear) equations x*B = A for x. – MeMyselfAndI Sep 29 '14 at 10:43
  • I took the liberty of moving your explanation into the solution. The question ends with "Can anyone explain this to me" so I think the correct solution should have an explanation and not just correct the error. – Dan Sep 29 '14 at 15:03