Azzera filtri
Azzera filtri

How to write m file

3 visualizzazioni (ultimi 30 giorni)
Greg Thomas
Greg Thomas il 18 Feb 2013
I need help writing a m file function. Any pointers would be great. Thank you.

Risposta accettata

Image Analyst
Image Analyst il 18 Feb 2013
There are so many ways to do this. Probably the easiest way for a beginner is the for loop way. Hint:
for tIndex = 1 : length(t)
theSum = a(1);
for k = 2 : length(a)-1
theSum = theSum + a(k) * cos((k-1)*t(tIndex)) + b(k) * sin((k-1)*t(tIndex));
end
y(tIndex) = %%%%%%%%%%% You have to do something to finish your homework
end
Since it's your homework I probably did too much for you, but I didn't do it 100% so at least you can claim some small part as your own.
  2 Commenti
Image Analyst
Image Analyst il 18 Feb 2013
Modificato: Image Analyst il 18 Feb 2013
No - that's not right. A function that you call from another m-file or the command line has to start with a function keyword. But the rest of what you did was all wrong. The y= line is just an example, like an equation you'd find in a book - it's not MATLAB code. The a= and b= lines are what you're supposed to issue at the command line, or in a script (a separate m-file), not in this one you're supposed to write. Next, you didn't use any of what I showed you - you totally ignored it. Finally, the t= line is wrong. You're not supposed to pass your coefficients (a and b) into linspace() - you're supposed to do exactly what they said:
t = linspace(0,4*pi,500);
on the command line (after a= and b=) or in your script (again, after the a= line and b= line).
I'll give you one more hint. Make an m-file called "part_sum_fs.m" and in it have this:
function y = part_sum_fs(a, b, t)
for tIndex = 1 : length(t)
theSum = a(1);
for k = 2 : length(a)-1
theSum = theSum + a(k) * cos((k-1)*t(tIndex)) + b(k) * sin((k-1)*t(tIndex));
end
y(tIndex) = %%%%%%%%%%% You have to do something to finish your homework
end
return; % from part_sum_fs()
All you need to do is add one word and you're done. Think about what glaring obviously thing it might possibly be.
Then, make a script to test your function called "test_part_sum_fs.m" and in it you have this:
a = [1, 2, 0, 0];
b = [3, 0,-1, 0];
t = linspace(0,4*pi,500); % Makes an array for t
y = part_sum_fs(a,b,t); % Returns an array for y
plot(t,y)
xlabel('t');
ylabel('y');
Then you run that script, which will call your function. title('y=1+2cos(t)+3sin(t)-sin(3t)')
If you want to get fancy, you can program it to handle different length arrays for a and b, like they did. I just added a zero to the last, missing b value to make it easy.
Image Analyst
Image Analyst il 19 Feb 2013
You're welcome. You'll get better with practice. Just look over the FAQ, and search Answers for "tag:tutorial" for some good advice. And of course look in my File Exchange and run demos I post here. In about 3 months you should be up to nearly full speed and be able to program most things with little hesitation.

Accedi per commentare.

Più risposte (1)

Alessandro Renna
Alessandro Renna il 18 Feb 2013
try this:
function [y] = part_sum_fs(a,b,t)
a_0=a(1); a_1=a(2); a_2=a(3);
b_1=b(1); b_2=b(2);
y=a_0+a_1*cos(t)+b_1*sin(t)+a_2*cos(2*t)+b_2*sin(2*t);

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by