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.
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);
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.