In an assignment A(I) = B, the number of elements in B and I must be the same. Error in odev_3_c (line 9) fprime(1)=x.*cos(3*x); >>

clc
clear
format long
x=0:1:4;
fg=x.*(cos(3*x));
% 6 anlamlı basamak için hesaplama
fprime(1)=x.*cos(3*x);
fprime(2)=-6*sin(3*x)-9*x.cos(3*x);
fprime(3)=-27*cos(3*x)+27*x.*sin(3*x);
fprime(4)=108*sin(3*x)+81*x.cos(3*x);
fprime(5)=405*cos(3*x)-243*x.*sin(3*x);
fs1=0;
for i=1:5
fs1=fs1+fprime(i)*x^{i-1}/factorial(i-1);
%bağıl hata hesaplama
if abs((fg-fs1)/fg)<0.5*1e-5
disp('gercek değer:')
fg,
disp('aranan yaklaşık değer:')
fs1,
disp('iterasyon sayısı')
i,
break
end
end

 Accepted Answer

You defined x to be a vector of values x=0:1:4;, which means that x.*cos(3*x) will also be a vector of values. But you try to put a vector (i.e. multiple elements) into one element:
fprime(1)=x.*cos(3*x);
because fprime(1) is only one element, not multiple elements. If you do a search of this forum for this exact error message you will find thousands of explanations of why this happens: it is always because someone tries to put multiple elements into one element of a numeric array.
You should learn about matrix indexing:

3 Comments

You can use
fprime(1,:) = ...
to assign a vector.
BTW: *x.cos should read x.*cos
thank you but now another problem is occured;
Attempt to reference field of non-structure array.
Error in odev_3_c (line 11) fprime(3,:)=-6*sin(3*x)-9*x.cos(3*x);
clc
clear
format long
x=0.6283185;
fg=x.*(cos(3*x));
% 6 anlamlı basamak için hesaplama
fprime(1,:)=x.*cos(3*x);
fprime(2,:)=x.*cos(3*x);
fprime(3,:)=-6*sin(3*x)-9*x.cos(3*x);
fprime(4,:)=-27*cos(3*x)+27*x.*sin(3*x);
fprime(5,:)=108*sin(3*x)+81*x.cos(3*x);
fprime(6,:)=405*cos(3*x)-243*x.*sin(3*x);
fs1=0;
for i=1:5
fs1=fs1+fprime(i)*x^{i-1}/factorial(i-1);
%bağıl hata hesaplama
if abs((fg-fs1)/fg)<0.5*1e-5
disp('gercek değer:')
fg,
disp('aranan yaklaşık değer:')
fs1,
disp('iterasyon sayısı')
i,
break
end
end
Thorsten already told you the answer to this. Here it is again: x.cos should read x.*cos.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!