please freakin' help!

edited November 2010 in Science & Tech
I will seriously pay someone to help me. I need help with user defined functions and calling them.

for this problem, I am supposed to write a function that calculates y=x^2 if icalc=1 and y=sqrt(x) if icalc=2. Then I call it and use fprintf to display y. X=[1 4 9] and icalc is taken from user.

Here is my code for the function:
[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]
function[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] y=f_calc(icalc)
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]global[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] x
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] icalc==1
y=x^2
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] icalc==2
y=sqrt(x)
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]end
end

Here is my code for the calling program:

[/FONT]
[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]x=[1 4 9]
icalc=input([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a020f0][FONT=Courier New][SIZE=2][COLOR=#a020f0][FONT=Courier New][SIZE=2][COLOR=#a020f0]'Enter icalc:'[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])
y=f_calc(icalc)
fprintf([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a020f0][FONT=Courier New][SIZE=2][COLOR=#a020f0][FONT=Courier New][SIZE=2][COLOR=#a020f0]'\n%d'[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2],y)[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]
[/SIZE][/SIZE][/FONT]


Please please please help and check out my other thread. Just need some hints or something please.
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]

Comments

  • drasnordrasnor Starship Operator Hawthorne, CA Icrontian
    edited November 2010
    Globals are never a good idea in function M files. Your function's argument list should have everything you need to run the function, e.g. function y = f_calc(x, icalc). Also, you need to use an element-wise power operator if you want to square each element of X ( .^ instead of ^ ). Your code will behave unpredictably if you give it an icalc value other than 1 or 2; you may want to replace the elseif statement with an else or say else y=0; disp("ICALC out of range"); or something.
  • RyderRyder Kalamazoo, Mi Icrontian
    edited November 2010
    Same question as your other thread? http://icrontic.com/forum/showthread.php?t=91156
  • edited November 2010
    thanks so much for help in both questions. For these particular questions, I am required to use global memory. I'm going to get to work on these problems armed with the feedback and see what I can finish.

    Thanks again
Sign In or Register to comment.