My code will not run after adding in the code after function dydt=myFunc(t,y)

1 view (last 30 days)
%clear,clc
%why is call_myFunc not read
Q_A=0;
P_1=0;
Q_B=0;
P_2=0;
[y(1),y(2),y(3),y(4)]=deal(Q_A,P_1,Q_B,P_2);
[T,Y]=call_myFunc;
Array indices must be positive integers or logical values.

Error in solution>myFunc (line 29)
deltaP(t)=A*sin(w*t)+B;

Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 106)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

Error in solution>call_myFunc (line 12)
[T,Y]=ode45(@myFunc,tspan,ICs);
function [T,Y]=call_myFunc()
tspan=[0 8*pi];
ICs=[0,0,0,0];
[T,Y]=ode45(@myFunc,tspan,ICs);
figure(1)
plot(T,Y(:,1),'b',T,Y(:,2),'k', T,Y(:,3),'g', T,Y(:,4),'m')
legend('Q_A','P_1','Q_B','P_2')
xlabel('time[s]')
end
function dydt=myFunc(t,y)
dydt=zeros(4,1);
Ra=1;
Rb=1;
La=0.1;
Lb=0.1;
C1=1;
C2=1;
A=30; %mmHg
B=90; %mmHg
w=2*pi; %rad/s
deltaP(t)=A*sin(w*t)+B;
dydt=[-(Ra/La)*y(1)-(1/La)*y(2)+0*y(3)+(1/La)*y(4);(1/C1)*y(1)+0*y(2)-(1/C1)*y(3)+0*y(4);0*y(1)+(1/Lb)*y(2)-(Rb/Lb)*y(3)-(1/Lb)*y(4);-(1/C2)*y(1)+0*y(2)+(1/C2)+0*y(4)]
end

Answers (1)

Walter Roberson
Walter Roberson on 12 Oct 2021
function dydt=myFunc(t,y)
t will be a double precision number representing the current "time". t will seldom be an integer during execution
deltaP(t)=A*sin(w*t)+B;
In MATLAB, Variable(expression) on the left hand side of an assignment statement has two possible meanings:
  1. The expression represents an index into the given variable. Valid indices must be datatype logical, or (obscurely) must be a character or character vector (obscure!), or must be numeric values that are positive integers. Your t is a non-integer double precision scalar, and so is not a valid index; OR
  2. The expression is a symbolic variable name or (obscurely) a vector of symbolic variable names. In this case, the name does not indicate indexing, and instead indicates that a symbolic function is to be created in which that name is the name of the formal parameter; for example syms x; f(x) = x^2 - 2; In this case, the resulting symbolic function defines a formula rather than indexing
You appear to have tried to use formula definition syntax with a double precision number instead of a symbolic variable.
In this case, the repair is to remove the line. You do not use deltaP in your calculation, so there is no point assigning to it.
The more general solutions would be one of:
deltaP=A*sin(w*t)+B;
and then use deltaP as a variable name; OR
deltaP = @(t) A*sin(w*t)+B;
and then when you use deltaP be sure to pass a parameter to it.
I would suggest that using a plain variable is more likely to be useful for your situation.
Note: you also do not use a number of other variables in your function, such as A B w.

Tags

Products

Community Treasure Hunt

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

Start Hunting!