Plot Error: Vectors should be the same length

So basically guys, please help.
I don't understand, I am writing a script which should plot a graph.
When I use it independently, it works: (here is the script)
x = -10:0.01:10;
plot(x,q1,'g',x,q2,'r'),
axis([-3 3 -3 3])
x = linspace(c(1),c(2),10);
qq1 = -0.34+1.69*(x-0.25).^2;
qq2 = 1.76-0.77*(x-0.71).^2;
xb = [x,x];
yb = [qq1,qq2];
fill(xb,yb,'b')
hold on
xr = [x,x];
yr = [qq2,qq1];
fill(xr,yr,'b')
legend('q1 (x)', 'q2 (x)'),
xlabel('x'), ylabel('q (x)'),
However, the problem arises when I put it together With the rest of the script, sym x;
q1 = -0.34+1.69*(x-0.25).^2;
q2 = 1.76-0.77*(x-0.71).^2;
%find the points of intersection
syms x
equation = -0.34+1.69*(x-0.25).^2 - 1.76+0.77*(x-0.71).^2 == 0;
S = solve(equation,x);
c = double(S)
%find the area of the intersection
f = -0.34+1.69*(x-0.25).^2 - 1.76+0.77*(x-0.71).^2;
z = int(f, c(1), c(2));
w = abs(z);
display('Area: '), disp(double(w));
%plot the graph
x = -10:0.01:10;
plot(x,q1,'g',x,q2,'r'),
axis([-3 3 -3 3])
x = linspace(c(1),c(2),10);
qq1 = -0.34+1.69*(x-0.25).^2;
qq2 = 1.76-0.77*(x-0.71).^2;
xb = [x,x];
yb = [qq1,qq2];
fill(xb,yb,'b')
hold on
xr = [x,x];
yr = [qq2,qq1];
fill(xr,yr,'b')
legend('q1 (x)', 'q2 (x)'),
xlabel('x'), ylabel('q (x)'),
and I run the script and get this error:
Error using plot
Vectors must be the same length.
Error in Matlab_01 (line 21)
plot(x,q1,'g',x,q2,'r'),
Can somebody please help find where I am making the mistake? Thank you beforehand.

1 个评论

Please format code in a Code { } block in future. It makes it so much easier to read. And whilst I am in favour of blank lines around code to break it up I think every other line being blank does not aid readability!

请先登录,再进行评论。

回答(2 个)

Adam
Adam 2017-3-24
编辑:Adam 2017-3-24
You seem to keep reusing 'x'. I see 3 different places where it is redefined. The final one seems to make it a vector of length 10, whereas q1 is of an unknown length as it appears to be the first line of the script based on whatever x was at the time which might well have been:
x = -10:0.01:10;
You can use the debugger to very easily see these things. Either with a breakpoint on the relevant line you want to stop at or using the 'Stop on errors' option from the Breakpoints menu.
Try this:
figure
fplot(q1, [min(x) max(x)], 'g')
hold on
fplot(q2, [min(x) max(x)], 'r')
hold off
axis([-3 3 -3 3])
Use ezplot if you do not have fplot. They should produce the same results here.

类别

帮助中心File Exchange 中查找有关 Spline Postprocessing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by