how to solv this issue?

You must write a function called savefunc(name,func,grd) whose function is to display a graph that displays data from a random range.
example:

6 件のコメント

Image Analyst
Image Analyst 2023 年 1 月 2 日
Why must I do that?
This looks like your homework problem.
Is the assignment "Post the question on the Answers forum and take a solution someone posted there and submit it as your answer."?
If you have any questions ask your instructor or read the link below to get started:
Obviously we can't give you the full solution because you're not allowed to turn in our code as your own.
daniel
daniel 2023 年 1 月 2 日
function savefunc(file,fx,grd)
range=sort(rand(1,2));
x=linspace(range(1),range(2), 100);
y=eval(fx);
plot(x,y)
switch grd
case 'on'
grid on;
case 'off'
grid off;
end
daniel
daniel 2023 年 1 月 2 日
here is my code
Image Analyst
Image Analyst 2023 年 1 月 2 日
OK, so did it work when you passed in the arguments they told you to pass in?
daniel
daniel 2023 年 1 月 2 日
no its not working
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 1 月 2 日
編集済み: Sulaymon Eshkabilov 2023 年 1 月 2 日
Share you have for these input args: file,fx,grd

サインインしてコメントする。

回答 (5 件)

Jan
Jan 2023 年 1 月 2 日

0 投票

Replace
y=eval(fx);
by
y = fx(x)
and
switch grd
case 'on'
grid on;
case 'off'
grid off;
end
by the easier:
grid(grd)

1 件のコメント

Walter Roberson
Walter Roberson 2023 年 1 月 2 日
Looks like they expect a character vector for the function.

サインインしてコメントする。

Image Analyst
Image Analyst 2023 年 1 月 2 日
編集済み: Image Analyst 2023 年 1 月 3 日

0 投票

Your code runs without error:
savefunc('test', 'x.*sin(x)', 'off')
function savefunc(file,fx,grd)
range=sort(100 * rand(1,2));
x=linspace(range(1),range(2), 100);
y=eval(fx);
plot(x,y)
switch grd
case 'on'
grid on;
case 'off'
grid off;
end
caption = sprintf('The function range is between %.4f <= x <= %.4f', range(1), range(2));
title(caption)
xlabel('x')
ylabel('y')
end
To match the picture you'll need to change the range to 50 to 83.
You probably just never executed the command
>> savefunc('test', 'x.*sin(x)', 'off')
in the command window.
Also range is a built-in function.
daniel
daniel 2023 年 1 月 2 日

0 投票

3 件のコメント

Torsten
Torsten 2023 年 1 月 2 日
Did you save your function as "savefunc.m" in your working directory ?
daniel
daniel 2023 年 1 月 2 日
no
Torsten
Torsten 2023 年 1 月 2 日
Then do it. MATLAB searches for savefunc.m, but cannot find it anywhere.

サインインしてコメントする。

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 1 月 2 日

0 投票

Here is the corrected code:
savefunc('Test', 'x.*sin(x)', 'off')
function savefunc(file,fx, grd)
fileNAME=file;
f = fx;
range=sort(rand(1,2))*100;
x=linspace(range(1), range(2), 1000);
y=eval(f);
plot(x,y)
xlabel('x'), ylabel(f),
switch grd
case 'on'
grid on;
case 'off'
grid off;
end
title(['The function range between: ' num2str(x(1)) '< x < ' num2str(x(2))])
D = [x.' y.'];
writematrix(D, strcat([fileNAME '.txt'])); % Simulation data is stored
end

2 件のコメント

daniel
daniel 2023 年 1 月 2 日
same messege
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 1 月 2 日
Your are executig the fcn incorrectly. Run the code as this way from one M-file eg.:
savefunc('Test', 'x.*sin(x)', 'off')
function savefunc(file,fx, grd)
fileNAME=file;
f = fx;
range=sort(rand(1,2))*100;
x=linspace(range(1), range(2), 1000);
y=eval(f);
plot(x,y)
xlabel('x'), ylabel(f),
switch grd
case 'on'
grid on;
case 'off'
grid off;
end
title(['The function range between: ' num2str(x(1)) '< x < ' num2str(x(2))])
D = [x.' y.'];
writematrix(D, strcat([fileNAME '.txt'])); % Simulation data is stored
end
or recall the fucntion file from the command window, e.g:
function savefunc(file,fx, grd) % saved M-file called savefcn the resides in your current MATLAB directory
fileNAME=file;
f = fx;
range=sort(rand(1,2))*100;
x=linspace(range(1), range(2), 1000);
y=eval(f);
plot(x,y)
xlabel('x'), ylabel(f),
switch grd
case 'on'
grid on;
case 'off'
grid off;
end
title(['The function range between: ' num2str(x(1)) '< x < ' num2str(x(2))])
D = [x.' y.'];
writematrix(D, strcat([fileNAME '.txt'])); % Simulation data is stored
end
Call it from the command window:
>> savefunc('Test', 'x.*sin(x)', 'off')

サインインしてコメントする。

Walter Roberson
Walter Roberson 2023 年 1 月 2 日

0 投票

there is no savefunc() built into matlab or any of the toolboxes or any of the file exchange contributions. Your task is create your own function for it.
There is no particular location where you must save the function file, but it must exist in your current directory or somewhere along the matlab "path" (see pathtool). There are, though, some locations best avoided:
  • directories named "resources" cannot be used
  • if the directory is named "private" then the function can only be called by functions in the parent directory
  • if the directory or a parent directory is named starting with a + then there are special ways to invoke the function
  • if the directory or a parent directory is named starting with a @ then it is part of a class definition and special ways of calling it might apply
  • do not store your code anywhere under the MATLAB installation directory; there may be security restrictions or matlab might refuse to recognize the file unless you use special steps, or in some cases might say that you need the license from a different toolbox (some directory names are magic inside the matlab installation directory)

カテゴリ

ヘルプ センター および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

質問済み:

2023 年 1 月 2 日

編集済み:

2023 年 1 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by