Parse error at observed data (2024)

29 views (last 30 days)

Show older comments

Abdullahi on 10 Aug 2024 at 8:41

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2144369-parse-error-at-observed-data

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2144369-parse-error-at-observed-data

Answered: Torsten on 10 Aug 2024 at 18:17

Open in MATLAB Online

% Define the SITR model as a system of ODEs

function dydt = SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N)

S = y(1); % Susceptible

I = y(2); % Infectious

Q = y(3); % Isolated

T = y(4); % Treated

R = y(5); % Recovered

dSdt = -beta * S * I / N;

dIdt = beta * S * I / N - gamma * I - delta * I - alpha * I;

dQdt = delta * I - lambda * Q;

dTdt = alpha * I - mu * T;

dRdt = gamma * I + lambda * Q + mu * T;

dydt = [dSdt; dIdt; dQdt; dTdt; dRdt];

end

% Observed data (replace with actual data)

% Format: [time, infected, isolated, treated, recovered]

observed_data = [

0, 1, 0, 0, 0;

10, 50, 10, 5, 15;

20, 100, 25, 15, 50;

30, 150, 35, 30, 100;

40, 200, 50, 50, 200

];

% Initial conditions

N = 1000000; % Total population

S0 = N - 1;

I0 = 1;

Q0 = 0;

T0 = 0;

R0 = 0;

y0 = [S0, I0, Q0, T0, R0];

% Time points for the solution (based on observed data)

tspan = observed_data(:, 1);

% Define the objective function for optimization

function error = objectiveFunction(params)

beta = params(1);

gamma = params(2);

delta = params(3);

alpha = params(4);

lambda = params(5);

mu = params(6);

% Solve the ODE with the current parameters

[t, y] = ode45(@(t, y) SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N), tspan, y0);

% Interpolate the model's output to match the time points of observed data

model_values = interp1(t, y(:, 2:5), tspan);

% Calculate the sum of squared errors

error = sum((model_values - observed_data(:, 2:5)).^2, 'all');

end

% Initial guess for parameters [beta, gamma, delta, alpha, lambda, mu]

initial_params = [0.3, 0.1, 0.05, 0.02, 0.1, 0.1];

% Perform optimization using fminsearch

optimized_params = fminsearch(@objectiveFunction, initial_params, options);

Unrecognized function or variable 'tspan'.

Error in solution>objectiveFunction (line 50)
[t, y] = ode45(@(t, y) SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N), tspan, y0);

Error in fminsearch (line 209)
fv(:,1) = funfcn(x,varargin{:});

% Display optimized parameters

disp('Optimized parameters:');

disp(optimized_params);

% Solve the system with optimized parameters

[t, y] = ode45(@(t, y) SITRModel(t, y, optimized_params(1), optimized_params(2), optimized_params(3), optimized_params(4), optimized_params(5), optimized_params(6), N), linspace(0, 160, 100), y0);

% Plot the solution with optimized parameters

figure;

plot(t, y(:, 1), 'b-', t, y(:, 2), 'r-', t, y(:, 3), 'g-', t, y(:, 4), 'm-', t, y(:, 5), 'k-');

legend('Susceptible', 'Infectious', 'Isolated', 'Treated', 'Recovered');

title('Fitted SITR Model for COVID-19');

xlabel('Days');

ylabel('Population');

grid on;

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (2)

Star Strider on 10 Aug 2024 at 13:08

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2144369-parse-error-at-observed-data#answer_1497404

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2144369-parse-error-at-observed-data#answer_1497404

Open in MATLAB Online

It took a few minutes to determine what the problems are with this (there were several).

I got it to work. Most of the problems were with respect to passing all the necessary arguments to the various funcitons. I will let you explore the changes I made, probably the most important of which was using ‘@SITRModel’ to pass the function as an argument.

Try this —

% Define the SITR model as a system of ODEs

function dydt = SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N)

S = y(1); % Susceptible

I = y(2); % Infectious

Q = y(3); % Isolated

T = y(4); % Treated

R = y(5); % Recovered

dSdt = -beta * S * I / N;

dIdt = beta * S * I / N - gamma * I - delta * I - alpha * I;

dQdt = delta * I - lambda * Q;

dTdt = alpha * I - mu * T;

dRdt = gamma * I + lambda * Q + mu * T;

dydt = [dSdt; dIdt; dQdt; dTdt; dRdt];

end

% Observed data (replace with actual data)

% Format: [time, infected, isolated, treated, recovered]

observed_data = [0, 1, 0, 0, 0;

10, 50, 10, 5, 15;

20, 100, 25, 15, 50;

30, 150, 35, 30, 100;

40, 200, 50, 50, 200];

% Initial conditions

N = 1000000; % Total population

S0 = N - 1;

I0 = 1;

Q0 = 0;

T0 = 0;

R0 = 0;

y0 = [S0, I0, Q0, T0, R0];

% Time points for the solution (based on observed data)

tspan = observed_data(:, 1);

% Define the objective function for optimization

function error = objectiveFunction(params,tspan,SITRModel,y0,N,observed_data)

beta = params(1);

gamma = params(2);

delta = params(3);

alpha = params(4);

lambda = params(5);

mu = params(6);

% Solve the ODE with the current parameters

[t, y] = ode45(@(t, y) SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N), tspan, y0);

% Interpolate the model's output to match the time points of observed data

model_values = interp1(t, y(:, 2:5), tspan);

% Calculate the sum of squared errors

error = sum((model_values - observed_data(:, 2:5)).^2, 'all');

end

% % Initial guess for parameters [beta, gamma, delta, alpha, lambda, mu]

initial_params = [0.3, 0.1, 0.05, 0.02, 0.1, 0.1];

initial_params = initial_params + rand(1,6)/100

initial_params = 1x6

0.3046 0.1054 0.0536 0.0203 0.1001 0.1038

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

% % Perform optimization using fminsearch

options = optimset('MaxFunEvals', 1000, 'MaxIter', 1000);

optimized_params = fminsearch(@(params)objectiveFunction(params,tspan,@SITRModel,y0,N,observed_data), initial_params, options);

% Display optimized parameters

disp('Optimized parameters:');

Optimized parameters:

disp(optimized_params);

0.3375 0.1071 0.0483 0.0476 0.0501 0.0536

% optimized_params = initial_params;

% Solve the system with optimized parameters

[t, y] = ode45(@(t, y) SITRModel(t, y, optimized_params(1), optimized_params(2), optimized_params(3), optimized_params(4), optimized_params(5), optimized_params(6), N), linspace(0, 160, 100), y0);

% Plot the solution with optimized parameters

figure;

plot(t, y(:, 1), 'b-', t, y(:, 2), 'r-', t, y(:, 3), 'g-', t, y(:, 4), 'm-', t, y(:, 5), 'k-');

legend('Susceptible', 'Infectious', 'Isolated', 'Treated', 'Recovered');

title('Fitted SITR Model for COVID-19');

xlabel('Days');

ylabel('Population');

grid on;

Parse error at observed data (3)

.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Torsten on 10 Aug 2024 at 18:17

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2144369-parse-error-at-observed-data#answer_1497494

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2144369-parse-error-at-observed-data#answer_1497494

Open in MATLAB Online

% Observed data (replace with actual data)

% Format: [time, infected, isolated, treated, recovered]

observed_data = [

0, 1, 0, 0, 0;

10, 50, 10, 5, 15;

20, 100, 25, 15, 50;

30, 150, 35, 30, 100;

40, 200, 50, 50, 200

];

% Initial conditions

N = 1000000; % Total population

S0 = N - 1;

I0 = 1;

Q0 = 0;

T0 = 0;

R0 = 0;

y0 = [S0, I0, Q0, T0, R0];

% Time points for the solution (based on observed data)

tspan = observed_data(:, 1);

% Initial guess for parameters [beta, gamma, delta, alpha, lambda, mu]

initial_params = [0.3, 0.1, 0.05, 0.02, 0.1, 0.1];

objectiveFunction(initial_params,y0,N,observed_data)

ans = 2.8078e+04

% Perform optimization using fminsearch

options = optimset('MaxFunEvals', 10000, 'MaxIter', 10000);

%optimized_params = fminsearch(@(params)objectiveFunction(params,y0,N,observed_data), initial_params, options);

optimized_params = fmincon(@(params)objectiveFunction(params,y0,N,observed_data), initial_params, [],[],[],[],zeros(6,1),[],[],options);

Local minimum possible. Constraints satisfied.fmincon stopped because the size of the current step is less thanthe value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.

% Display optimized parameters

disp('Optimized parameters:');

Optimized parameters:

disp(optimized_params);

0.3375 0.1334 0.0354 0.0342 0.0000 0.0000

objectiveFunction(optimized_params,y0,N,observed_data)

ans = 2.3540e+04

% Solve the system with optimized parameters

[t, y] = ode15s(@(t, y) SITRModel(t, y, optimized_params,N), linspace(0,160,100), y0,odeset('RelTol',1e-9,'AbsTol',1e-9));

% Plot the solution with optimized parameters

figure;

plot(t, y(:, 1), 'b-', t, y(:, 2), 'r-', t, y(:, 3), 'g-', t, y(:, 4), 'm-', t, y(:, 5), 'k-');

legend('Susceptible', 'Infectious', 'Isolated', 'Treated', 'Recovered');

title('Fitted SITR Model for COVID-19');

xlabel('Days');

ylabel('Population');

grid on;

Parse error at observed data (5)

% Define the objective function for optimization

function error = objectiveFunction(params,y0,N,observed_data)

% Solve the ODE with the current parameters

[t, y] = ode15s(@(t, y) SITRModel(t, y, params,N), observed_data(:,1), y0,odeset('RelTol',1e-9,'AbsTol',1e-9));

% Calculate the sum of squared errors

error = sum((y(:,2:5) - observed_data(:,2:5)).^2, 'all');

end

% Define the SITR model as a system of ODEs

function dydt = SITRModel(t, y, params,N)

beta = params(1);

gamma = params(2);

delta = params(3);

alpha = params(4);

lambda = params(5);

mu = params(6);

S = y(1); % Susceptible

I = y(2); % Infectious

Q = y(3); % Isolated

T = y(4); % Treated

R = y(5); % Recovered

dSdt = -beta * S * I / N;

dIdt = beta * S * I / N - gamma * I - delta * I - alpha * I;

dQdt = delta * I - lambda * Q;

dTdt = alpha * I - mu * T;

dRdt = gamma * I + lambda * Q + mu * T;

dydt = [dSdt; dIdt; dQdt; dTdt; dRdt];

end

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

RF and Mixed SignalAntenna ToolboxAntenna CatalogMonopole Antennas

Find more on Monopole Antennas in Help Center and File Exchange

Tags

  • parse error

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Parse error at observed data (6)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Parse error at observed data (2024)
Top Articles
Single White Female (1992)
‘Single White Female’ at Thirty: The Legacy of the Women Stalkers of the 1990s 
Artem The Gambler
Tlc Africa Deaths 2021
Belle Meade Barbershop | Uncle Classic Barbershop | Nashville Barbers
Vaya Timeclock
craigslist: south coast jobs, apartments, for sale, services, community, and events
Carter Joseph Hopf
Mawal Gameroom Download
83600 Block Of 11Th Street East Palmdale Ca
Athens Bucket List: 20 Best Things to Do in Athens, Greece
Hood County Buy Sell And Trade
Games Like Mythic Manor
Enterprise Car Sales Jacksonville Used Cars
Jenn Pellegrino Photos
Sonic Fan Games Hq
Blue Rain Lubbock
Orange Pill 44 291
Bella Bodhi [Model] - Bio, Height, Body Stats, Family, Career and Net Worth 
Nesb Routing Number
Asteroid City Showtimes Near Violet Crown Charlottesville
What Equals 16
Student Portal Stvt
Blackboard Login Pjc
Temu Seat Covers
Effingham Daily News Police Report
Toonkor211
Greyson Alexander Thorn
Hannah Jewell
Ravens 24X7 Forum
Mg Char Grill
Newsday Brains Only
Terrier Hockey Blog
Page 5662 – Christianity Today
Duff Tuff
Bismarck Mandan Mugshots
10 games with New Game Plus modes so good you simply have to play them twice
Omaha Steaks Lava Cake Microwave Instructions
Union Corners Obgyn
התחבר/י או הירשם/הירשמי כדי לראות.
Cnp Tx Venmo
Mississippi weather man flees studio during tornado - video
Disassemble Malm Bed Frame
Frequently Asked Questions
552 Bus Schedule To Atlantic City
Sams La Habra Gas Price
Runelite Ground Markers
David Turner Evangelist Net Worth
sin city jili
Rise Meadville Reviews
Inloggen bij AH Sam - E-Overheid
La Fitness Oxford Valley Class Schedule
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 5961

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.