help with applications in numerical analysis

1 次查看(过去 30 天)
Hi, I am trying to figure out why is not plotting, this is the error that I get Error using ==> plot Vectors must be the same lengths. any advice?
if true
if true
i=1;
for q=0:0.05:4;
x(i)=q;
s(i)=quad('sin(x.^2)',0,x(i));
c(i)=quad('cos(x.^2)',0,x(i));
i=i+1;
end
fprintf('\n The value of s(x)=%5.3f',s(i-1))
fprintf('\n The value of c(x)=%5.3f\n',c(i-1))
plot(x,s,x,c)
xlabel('x')
ylabel(' C(x) and S(x)')
grid
figure
plot(c,s)
xlabel('c'),ylabel('s')
end

回答(2 个)

Andrei Bobrov
Andrei Bobrov 2013-7-24
the code will work.
other variant:
q=0:0.05:4;
s = arrayfun(@(z)quad(@(z)sin(z^2),0,z),q);
c = arrayfun(@(z)quad(@(z)cos(z^2),0,z),q);
plot(q,[s;c]);
figure,plot(c,s);

Jan
Jan 2013-7-24
编辑:Jan 2013-7-24
The code inserts new values to an eventually existing variable. A pre-allocation would solve this:
x = 0:0.04:4;
n = length(x);
s = zeros(1, n); % Pre-allocate and replace existing x, s and c
c = zeros(1, n);
for k = 1:n
s(k) = quad('sin(x.^2)', 0, x(k));
c(k) = quad('cos(x.^2)', 0, x(k));
end

类别

Help CenterFile Exchange 中查找有关 Interactions, Camera Views, and Lighting 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by