Wrong output with MATLAB S Function (2024)

26 views (last 30 days)

Show older comments

AYYAPPA RUDRASIMHA on 28 Aug 2024 at 13:06

  • Link

    Direct link to this question

    https://in.mathworks.com/matlabcentral/answers/2148404-wrong-output-with-matlab-s-function

  • Link

    Direct link to this question

    https://in.mathworks.com/matlabcentral/answers/2148404-wrong-output-with-matlab-s-function

Answered: Divyajyoti Nayak on 30 Aug 2024 at 7:27

  • SOGI_S_Function.zip

When I implement simple SOGI block to generate quadrature signal for given sinusoidla input, output magnitude comming exactly half. SOGI will give two outputs one is alpha and another is beta component. Beta component magnitude and phase is correct but alpha component magnitude is comming exactly half. Same discrete equations when I implemented in matlab function or tf block in simulink its giving correct. Same equations and everything same but when I implemented in s function its giving wrong.

Correct output with MATLAB Function: (Valpha and vin both having same phase and maginitude)

Wrong output with MATLAB S Function (2)

Wrong output with S Function (C code): (Valpha and vin both having same phase and maginitude)

Wrong output with MATLAB S Function (3)

I attached the simulation files of s function, This error is only because of s function related issues. Please check and let me know.

Thanks in advance.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (1)

Divyajyoti Nayak on 30 Aug 2024 at 7:27

  • Link

    Direct link to this answer

    https://in.mathworks.com/matlabcentral/answers/2148404-wrong-output-with-matlab-s-function#answer_1507654

  • Link

    Direct link to this answer

    https://in.mathworks.com/matlabcentral/answers/2148404-wrong-output-with-matlab-s-function#answer_1507654

Open in MATLAB Online

Hi @AYYAPPA RUDRASIMHA,

The simulation results using the MATLAB function block and S-function block are different because the C code you have written for the S-function block is not equivalent to the code in MATLAB function block due to a small difference.

In the MATLAB function block, the ‘err’ variable is given the value of the input variable ‘ref’ after the assignment of variables ‘err_n2’and ‘err_n1’.

err_n2 = err_n1 ; %Initially Zero

err_n1 = err ; %Initially Zero because err = 0

err = ref; %The input ref is assigned to err

While in the S-function block, the equivalent variables ‘SOGI_v.vin’, ‘SOGI_v.vin_n1’, and ‘SOGI_v.vin_n2’ are handled differently. SOGI_v.vin is assigned the input variable ‘vin’ before ‘SOGI_v.vin_n1’ and ‘SOGI_v.vin_n2’ have been assigned. This makes the initial state of the variables different and hence leading to the wrong result.

SOGI_v.vin = vin; %Assigned before SOGI_v.vin_n1 and SOGI_v.vin_n2

SOGI_v.d0 = (4 + 2 * K * Ts * w + (w * w * Ts * Ts));

d0 = (4 + (2 * K * Ts * w) + (w * w * Ts * Ts));

SOGI_v.b1 = (-8 + (2 * w * w * Ts * Ts)) / SOGI_v.d0;

SOGI_v.b2 = ((4 - (2 * K * Ts * w) + (w * w * Ts * Ts))) / SOGI_v.d0;

SOGI_v.a_alpha = (2 * K * Ts * w) / SOGI_v.d0;

SOGI_v.a_beta = (K * w * w * Ts * Ts) / SOGI_v.d0;

b1 = (-8 + (2 * w * w * Ts * Ts)) / (4 + (2 * K * Ts * w) + (w * w * Ts * Ts));

b2 = ((4 - (2 * K * Ts * w) + (w * w * Ts * Ts))) / (4 + (2 * K * Ts * w) + (w * w * Ts * Ts));

a1 = (2 * K * Ts * w) / (4 + (2 * K * Ts * w) + (w * w * Ts * Ts));

SOGI_v.vin_n2 = SOGI_v.vin_n1; %Initially zero

SOGI_v.vin_n1 = SOGI_v.vin; %Not initially zero as vin is already assigned

To fix the issue, just write the assignment of ‘SOGI_v.vin’ after assigning ‘SOGI_v.vin_n2’ and ‘SOGI_v.vin_n1’ and it should give the correct result.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

Code GenerationEmbedded CoderDeployment, Integration, and Supported HardwareEmbedded Coder Supported HardwareTexas Instruments C2000 Processors

Find more on Texas Instruments C2000 Processors in Help Center and File Exchange

Tags

  • s-function
  • simulink
  • code
  • matlab coder
  • matlab function
  • embedded coder

Products

  • Simulink
  • Embedded Coder
  • MATLAB Coder

Release

R2023a

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.


Wrong output with MATLAB S Function (5)

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

Wrong output with MATLAB S Function (2024)
Top Articles
OCTAVIO VALDEZ JOINS UNIVISION LOS ANGELES AS ANCHOR OF NOTICIAS 34 AT 11PM - TelevisaUnivision
Univision Doubles Down on Spanish-Language Talk Radio in L.A. with New Programming Slate on KTNQ 1020-AM - TelevisaUnivision
Barstool Sports Gif
Syracuse Pets Craigslist
Terraria Artisan Loaf
C Chord for Ukulele: Variations, Styles, and Techniques
Zavvi Discount Code → 55% Off in September 2024
How Much Is Vivica Fox Worth
Miller Motte College Student Portal
Fairwinds Shred Fest 2023
Espn Masters Leaderboard
Mobile Maher Terminal
Blue Beetle Showtimes Near Regal Independence Plaza & Rpx
Ktbs Payroll Login
Craigslist Hoosick Falls
Newsweek Wordle
When Is Lana Rhoades’ Baby Due Date? Baby Daddy, Bump, And More
Craigslist North Platte Nebraska
SEBO (UK) Ltd on LinkedIn: #sebouk #commercialcleaning #cleaning #floorcleaning #carpetcleaning
Ups Access Point Location Georgetown Photos
Evertote.ca
Oh The Pawsibilities Salon & Stay Plano
10 018 Sqft To Acres
Sams Gas Price Garland Tx
Deleon Malik Taylor-Griffin
Stuckey Furniture
Www.cvs/Otchs/Simply
Back Doctor Near Me That Accept Medicaid
Geritol Complete - Gebrauchsanweisung, Dosierung, Zusammensetzung, Analoga, Nebenwirkungen / Pillintrip
Bollywood Movies 123Movies
Spn 102 Fmi 16 Dd15
Ontpress Fresh Updates
Slim Thug’s Wealth and Wellness: A Journey Beyond Music
Grand Forks (British Columbia) – Travel guide at Wikivoyage
Etfh Hatchery
Wisconsin Volleyball Team Leaked Pictures And Videos
Megan Eugenio Exposed
Expend4bles | Rotten Tomatoes
Denny's Ace Hardware Duluth Mn
M7 Bus
Sak Pase Rental Reviews
Comenity Bank Ann Taylor Loft
Wgu Admissions Login
Tacoma Craigslist Free
What Time Does The Chase Bank Close On Saturday
Aso Tools Vancouver
Dragith Nurn Rs3
Joann Stores Near Me
Dtm Urban Dictionary
Liberty 1098-T
EXTON: THE MOST BEAUTIFUL CHOCOLATE BOX VILLAGE IN RUTLAND
When His Eyes Opened Chapter 3002
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 6327

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.