help with applications in numerical analysis
Afficher commentaires plus anciens
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
1 commentaire
Narges M
le 24 Juil 2013
it should work fine.
Réponses (2)
Andrei Bobrov
le 24 Juil 2013
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);
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
Catégories
En savoir plus sur Interactions, Camera Views, and Lighting dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!