RMS value of periodic signal - using quad/quad8 command - getting an error with 'func' command

Hi, I'm trying to find the rms value of v(t)=2.5sin(2pi*10^6+30). I have one of my lecturers codes to use as an example but I keep running into the error:
"Undefined function or variable 'func'.
Error in quad (line 67)
y = f(x, varargin{:});"
Iv'e written out identically to him and tried making minor adjustements to fix it but nothing seems to work. Hears his example that we were givin to use.
clc
clear
T=10^(-6);
a=0;
b=T;
int_v_sq=quad('func',a,b);
v_rms_sq=int_v_sq/T;
v_rms=sqrt(v_rms_sq);
function y=func(t)
T=10^(-6);
phase=30;
phase_rad=phase*pi/180;
y=(2.5*sin(2*pi/T*t+phase_rad)).^2;
end
Any help would be much appreciated

 Accepted Answer

Use a function handle, just like the quad documentation states:
int_v_sq = quad(@func,a,b);

2 Comments

Thankyou so much Stephen, I'll forward this through to my lecturer aswell.
@James Friend: unfortunately some academics continue to write code just like when they first learned to use punched cards. Your starting reference should always be the MATLAB documentation.
In general using strings/character to store and refer to functions is strongly discouraged: the recommended way to refer to functions is to use function handles, which unlike strings are specifically designed for handling functions. Function handles were introduced around twenty years ago, so your lecturer is quite out of date.
The code in your question does not work because local functions, which includes those defined in a script, are only visible within the same file. But the quad function is, of course, defined in its own file, and so at the point in its code where it tries to call the function (provided as its first input) it cannot see the local function defined in another file (your script). In contrast the function handle is defined within your script and can then be called anywhere.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 6 Oct 2020

Edited:

on 6 Oct 2020

Community Treasure Hunt

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

Start Hunting!