please help me to convert c code into matlab code for partial shading of solar panel using equilibration algorithm

조회 수: 2 (최근 30일)
hei please convert this c code into matlab code
#include<stdio.h>
void main(){
float p1,p2,p3;
float delta=1;
scanf("%f%f%f",&p1,&p2,&p3);
float d = p2;
if(p1 != p2)
{
if(p1>p2)
{
if(p2>p3)
{
d=d-delta;
delta=delta*2;
}
else if(p2=p3)
{
d=d-delta;
delta=delta*2;
}
else
{
delta=delta/2;
}
}
else
{
if(p3>p2)
{
d=d+delta;
delta=delta*2;
}
else if(p2=p3)
{
d=d+delta;
delta=delta*2;
}
else
{
delta=delta/2;
}
}
}
else if(p1==p2)
{
if(p2>p3)
{
if(p2>p3)
{
d=d-delta;
delta=delta/2;
}
else if(p2<p3)
{
d=d-delta;
delta=delta*2;
}
else
{
delta=delta*2;
}
}
}
}

채택된 답변

Image Analyst
Image Analyst 2021년 6월 7일
You can do it.
  1. Replace } (closing braces) with end.
  2. Get rid of { (opening braces).
  3. Replace != with ~=.
  4. Get rid of float.
  5. Replace "else if" with "elseif" (no space)
Not sure what's going on here:
scanf("%f%f%f",&p1,&p2,&p3);
Come on, it's not beyond your capabilities. Give it a try. This is how you learn.

추가 답변 (1개)

James Tursa
James Tursa 2021년 6월 7일
편집: James Tursa 2021년 6월 7일
In addition to what @Image Analyst has written, I would advise turning this into a function with p1, p2, p3 as input arguments and d, delta as output arguments.
Also, the C code appears to have bugs. These lines:
else if(p2=p3)
should from context probably be this instead:
else if(p2==p3)
The p2=p3 expression is an assignment in C that is then tested for non-zero, not an equality test, so this expression will be true whenever p3 is non-zero. You should double check your algorithm to confirm this is a bug. The MATLAB code for an equality test should use the == operator also.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by