MATLAB: Dubble sin in first order Differential Equation

differential equations

Hi,
I'm very new to matlab so this might be a dumb question but,
I'm trying to solve a first order differential equation with a dubble sin,
but everytime I add a sin to the y matlab gives a "Invalid expression" error.
Does anyone know how to solve this problem?
h = 0.01;
begin = 0.0;
end = 5;
x = begin:h:end;
y = zeros(size(x));
y(1) = 2;
for i=1:numel(y)-1
f = sin(y(i).)*sin(x(i).^2)
y(i+1) = y(i) + h .* f;
end
plot(x,y);
grid on;

Best Answer

Should be like this
>> h = 0.01;
begin = 0.0;
finish = 5; % Don't use end here as that's a reserved word in MATLAB
x = begin:h:finish;
y = zeros(size(x));
y(1) = 2;
for i=1:numel(y)-1
f = sin(y(i)).*sin(x(i).^2); % note that you had .)* when you should have had ).*
y(i+1) = y(i) + h .* f;
end
plot(x,y);
grid on;
Related Question