Clear Filters
Clear Filters

problems of this function

2 views (last 30 days)
HADIMARGO
HADIMARGO on 14 Dec 2018
Reopened: HADIMARGO on 18 Dec 2018
i want to draw this function
but my code has an error:
code:
x=linspace(-5,5,1000);
n=double(1:1:100);
l=2;
f=sin(x);
g=cos(x);
s=0;
landa=((n*pi)/l );
a=(2/l)*int(f.*sin((n*pi)/l)*x,0,l);
b=(2/(l*landa))*int(g*sin((n*pi*x)/l,0,l));
for n=1:1:1000
p=((a*cos(landa*t)+ b*sin(landa*t))*sin((n*pi*x)/l));
s=s + p;
end
plot (x,t,s)
error:
>> f1
Matrix dimensions must agree.
Error in f1 (line 8)
a=(2/l)*int(f.*sin((n*pi)/l)*x,0,l);
where is the problem of this error and code?

Accepted Answer

Jan
Jan on 14 Dec 2018
Use the debugger to find the problem. Type this in the command window:
dbstop if error
Then run the code again until it stops at the error. Now split the command into parts and check them in the command window:
% a = (2 / l) * int(f .* sin((n * pi) / l) * x, 0, l);
size(2 / l)
size((n * pi) / l)
size(sin((n * pi) / l) * x) % I assume this fails
size(f)
size(f .* sin((n * pi) / l) * x)
This method let you identify, which part causes the error message. This should help you to fix the problem.
It looks strange that you define n as vector at first and use it a loop index again. I guess you want to calculate the integrals inside the loop.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!