help with applications in numerical analysis

조회 수: 1 (최근 30일)
francis
francis 2013년 7월 24일
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에서 Dependency Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by