Trying to solve Line 5: Parse error at x0: usage might be invalid MATLAB syntax.

I have a homework assignment and I'm just stuck on this one issue that the x0 is highlighted
so i cant run the code due to a parse error.
% Define initial conditions and time span
x0 = -2;
v0 = 0;
ic = [x0; v0];
tspan = [0, 10];
% Solve the equation using ode45
K = 10;
[t, x] = ode45(@(t,x) equation(t, x, K), tspan, ic);
% Plot the solution
plot(t, x(:,1))
xlabel('t')
ylabel('x')
title(['Solution to d^2x/dt^2 + 3dx/dt + (1/8+K)x=K with K=',num2str(K)])
grid on
function dxdt = equation(t, x, K)
dxdt = [x(2); -3*x(2) - (1/8+K)*x(1) + K];
end

 Accepted Answer

Works (see above).
Functions have to be placed after the script part of your code.

More Answers (0)

Products

Release

R2023a

Tags

Asked:

Joe
on 27 Apr 2023

Moved:

on 27 Apr 2023

Community Treasure Hunt

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

Start Hunting!