Matlab Function block changes input values by order of magnitude
1 view (last 30 days)
Show older comments
Hello!
I'm trying to implement a basic MPPT P&O Algorithm in Simulink. For this I have a Matlab Function block that takes in the current, voltage, and power. It then applies the P&O algorithm and outputs a desired dutycycle which is feed into a PWM generator.
The system was not behaving as I expected so I added a output form the Function Block
StateVector = [Ppv, Pprev, Vpv, Vprev, Dprev];
When then viewing the output from the StateVector with a scope I found that Ppv (INPUT) =/= Ppv (OUTPUT)
The full code (Incorrect Algorithm) is as follows:
function [D,DeltaP,StateVector] = fcn(Vpv,Ipv,Ppv)
persistent Dprev Pprev Vprev
if isempty(Dprev)
Dprev = 0.1;
Vprev = 48;
Pprev = 380;
end
%Ppv = Vpv*Ipv;
DeltaP = Ppv-Pprev;
deltaD = 0.1;
if (Ppv-Vprev) > 0
if (Vpv-Vprev) > 0
D = Dprev-deltaD;
else
D = Dprev+deltaD;
end
else
if (Vpv-Vprev) > 0
D = Dprev + deltaD;
else
D = Dprev - deltaD;
end
end
Dprev = D;
Vprev = Vpv;
Pprev = Ppv;
StateVector = [Ppv, Pprev, Vpv, Vprev, Dprev];
As you can see at no point is Ppv changed. At first it was calculated internally, but when I found this error I moved the computation outside making use of a product block.
The input Ppv can be seen below:

And the output:

I do not understand what could cause this. These signals are pulled directly at the input and output.
I appreciate any suggestions :)
EDIT: Ops the algorithm was very wrong. But ignoring the incorrectly formulated P&O Algorithm, why would the input-output relation be so strange?
2 Comments
Yifeng Tang
on 19 Mar 2025
Attaching the model will be helpful for the community to diagnose the issues.
Adarsh
on 27 Mar 2025
I have tried to reproduce the issue that you are facing using an example model, in which i have used the given code in the "MATLABFcn" block and provided the input ports with data generated by different signal generators, each having different frequencies (so that i can verify where exactly the issue is occuring). when I inspected the Input and the output "Ppv" signals in a "scope" block I see that both the signals are same, pointing no issue with the given function.
Here is an image of the example model that I have tested:

Here is the output of the "scope" block:

Attaching the exact model will be helpful to find out the exact issue.
Answers (0)
See Also
Categories
Find more on Programmatic Model Editing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!