Filter löschen
Filter löschen

help with applications in numerical analysis

2 Ansichten (letzte 30 Tage)
francis
francis am 24 Jul. 2013
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

Antworten (2)

Andrei Bobrov
Andrei Bobrov am 24 Jul. 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);

Jan
Jan am 24 Jul. 2013
Bearbeitet: Jan am 24 Jul. 2013
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

Kategorien

Mehr zu Modeling finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by