help with applications in numerical analysis

3 ビュー (過去 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
  1 件のコメント
Narges M
Narges M 2013 年 7 月 24 日
it should work fine.

サインインしてコメントする。

回答 (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 Center および File ExchangeInteractions, 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