# im having trouble with symfun here. it says that eqn1, k1, and k2 must be data of sym. how do i do that?

162 views (last 30 days)
Christella Bañaga on 29 Oct 2021
Commented: Walter Roberson on 8 Nov 2021
% Suppose a population of insect increases at a rate proportional to the amount present.
% There were 100 insects after the third day of the experiment and 250 insects after the seventh day.
% Approximately how many insects were in the original population? How many insects will there be after 24 days?
%Setup the variables P(t), k that will be used in the program. Find also the derivative of P(t) and set as dP
syms t k P(t);
dP = diff(P,t);
%Initial Conditions
cond1 = P(3)==100;
cond2 = P(7)==250;
%Set the differential equation model as eqn1;
eqn1 = dP-k*P==0;
P(t) = eqn1;
double(P(t));
%Find k1 and k2, by solving the initial value problem eqn1 using cond1 and cond2, respectively.
k1(t,k) = dsolve(eqn1,cond1);
k2(t,k) = dsolve(eqn1,cond2);
%Solve for k by equating k1 and k2 at t=0. Save results as k.
Sol(k) = k1(0,k)-k2(0,k);
eqn1 = Sol(k)==0;
k = solve(eqn1);
%Solve the eqn1 using the acquired value of k and using Initial value cond1.
Psoln(t) = k1(t,k);
x = linspace(-1,25,100);
y = Psoln(x);
%Evaluate Psoln at t=0, save your answer as P0. Express your answer in decimals, save answer as Po.
P0 = subs(Psoln(0));
Po = round(P0,2);
Po = double(Po);
%Evaluate Psoln at t=24, save your answer as P24. Express your answer in decimals, save answer as Po24.
P24 = subs(Psoln(24));
Po24 = round(P24,2);
%Plot the equation
x=0:0.2:30;
y=double(Psoln(x));
Title = "Growth Problem";
XValue = "Days";
YValue = "Number of Insects";
plot(x,y,'b--');
hold on;
title(Title);
xlabel(XValue);
ylabel(YValue);
plot(0,Psoln(0),'r*');
plot(24,Psoln(24),'r*');
hold off;
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 8 Nov 2021
Change
eqn1 = dP-k*P==0;
to
eqn1 = dP-k*P;
Change
k1(t,k) = dsolve(eqn1,cond1);
k2(t,k) = dsolve(eqn1,cond2);
to
k1 = dsolve(eqn1,cond1);
k2 = dsolve(eqn1,cond2);
And later instead of using k1 and k2 with parameters, subs() values for variables.

Gargi Patil on 8 Nov 2021
Edited: Gargi Patil on 8 Nov 2021
Hi,
I'm unable to reproduce the error you are facing. The code provided in the question is running without errors in MATLAB R2021b and MATLAB Online.
Based on the error messages, the assesment grader expects k1 and k2 to store symbolic values rather than equations (dsolve returns a differential equation). Similarly, eqn1 is expected to be a symbolic equation. Kindly verify the solution you are providing to the grader as this isn't an error due to MATLAB compilation.