1

i'm trying to insert an equation and plot it, but i couldn't because i keep getting errors like : matrix dimensions must agree, or inner matrix dimensions must agree.

http://www4.0zz0.com/2012/11/25/10/272913238.png this is the equation. M has a value of 1 to 6 with an increment of 0.5. Q has a value of 0 to 1 with an increment of 0.1.

http://www4.0zz0.com/2012/11/25/10/700692428.png the plot is something like this

 m=1:0.5:6;
 q=0:0.1:1;

i tried to split the equation into parts, so it would be easier for me to insert it, but i'm getting an error with the last part

e=q./m(1-sqrt(1-(q./m).^2));

Subscript indices must either be real positive integers or logicals.

illya
  • 765
  • 5
  • 8
Abdo Adam
  • 11
  • 1
  • Also see [this question](http://stackoverflow.com/questions/20054047/subscript-indices-must-either-be-real-positive-integers-or-logicals-generic-sol) for [the generic solution to this problem](http://stackoverflow.com/a/20054048/983722). – Dennis Jaheruddin Nov 27 '13 at 16:11

1 Answers1

1
  1. To iterate over each combination of m and q: you want to use ndgrid. Right now, both m and q are row vectors, so array-wise operations will only take the first element of m with the first element of q, the second element with the second element, and so on. What you want is a 2D matrix in which m varies along one dimension, and q varies along the other. This is what ndgrid does. Try this:

    [q, m] = ndgrid(0:0.1:1, 1:0.5:6);
    
  2. For the subscript indices error message: the problem is multiplication vs. array access. In the equation PNG, the denominator is of the form M{…}, which means M times the value in the braces. In your code, you write m(…), which is actually an array access — not a multiplication. Changing it to m .* (…) makes the code work. The working version is:

    e=q./(m.*(1-sqrt(1-(q./m).^2)));
    

    Now, you can do:

    figure; plot(e);
    

    …and you should get output similar to what you want.

illya
  • 765
  • 5
  • 8
  • thank you that helped, but the graph looks way different and i didn't manage to get multiple lines, i mean all i get is 1 line in the graph. which function should i use to get that type of graph. >> m=1:0.5:6; >> q=0:0.1:1; >> a=2*3.14; >> b=m; >> c=3.14; >> d=q./(2*m)-acos(sqrt(1-(q./m).^2)); >> e=q./(m.*(1-sqrt(1-(q./m).^2))); >> g=b.*(c.*d.*e); >> h=a./g; >> – Abdo Adam Nov 26 '12 at 03:36
  • Right — sorry. You want to use `ndgrid` for that part; updating my answer accordingly. – illya Nov 26 '12 at 03:51
  • i don't know what is the problem but still the graph is not the same, i thought maybe splitting the equation into parts is the reason, so i tried inserting the whole equation all at once, but i didn't work the graph still not the same. a=2*3.14./m.* (3.14+q./(2*m))-acos(sqrt(1-(q./m).^2))+q./(m.*(1-sqrt(1-(q./m).^2)‌​)); – Abdo Adam Nov 26 '12 at 04:56
  • @AbdoAdam, you have a couple problems with that equation. Any time you have something in the denominator, make sure that there are parentheses around it. The first problem is that you want the whole second half of the equation in the denominator of the main fraction, but as you have it written, only `m` is in the denominator. I tried to fix the equation, I think it should look more like this: `a=2*3.14./(m.* (3.14+q./(2*m)-acos(sqrt(1-(q./m).^2))+(m./q).*(1-sqrt(1-(q./m).^2))));`…but you should double-check me. – illya Nov 26 '12 at 13:35
  • @AbdoAdam Also, about the graph: I’m not sure how to interpret it. It looks like `M` is on the Y axis? And the X axis is a quantity `f/f0` — is that the same as A? – illya Nov 26 '12 at 13:36