How ı can write this function on Matlab
显示 更早的评论
采纳的回答
Hi,
Here is one of the possible solutions for x having a single value:
function Y = MY_fun(x)
if x>=1 && x<10
Y = exp(-x-1)/(log10(x)+log(x));
elseif x>=10
Y=sin(3*x)/sqrt(3*pi);
else
Y = x^(-exp(1)+x)+sqrt(x)*abs(x);
end
end
13 个评论
Homework well done!
thanks for your answer but its doesnt work
Error like that.....
Cannot find an exact (case-sensitive) match for 'soru3'
The closest match is: Soru3 in C:\Users\HP OMEN\Desktop\MATLAB\Soru3.m
Did you mean:
>> Soru3
where did you get: Soru3 or soru3
In the above code, there is no such function or command.
The posted code doesn't match the equations.
@Sulaymon: we discourage people from posting fully working solutions to homework problems, as that invites cheating.
If you want to allow array inputs: make sure the formulae themselves allow array operations (so .* etc). Pre-allocate an output vector. Then for every piece of the function create a logical mask. You can fill your output with y(L)=f(x(L));.
my file's name is Soru3
@Rik please can u show me how i can do this. because its dont work still :(
It is your homework, not mine. Show that you have put in some effort. You already have a good idea about how to solve it, because you have a nearly correct answer. What did you try?
thats right but im working about that. i didnt understand whats wrong ...
clc;
clear all;
function y(L)=f(x(L))
if x>=1 || x<10
Y = exp(-x-1)/(log10(x)+log(x));
elseif x>=10
Y=sin(3*x)/sqrt(3*pi);
else
Y = x^(-exp(1)+x)+sqrt(x)*abs(x);
end
end
You should only ever have exactly one script that contains "clear all" in your code base, as part of a script that is designed to reset MATLAB including closing all graphics and files and triggering garbage collection.
Having "clear all" in code is rather like Wile E. Coyote exploding the bridge underneath himself and expecting to stay safe in mid-air as long as he does not look down.
Your code does not ever call your function f.
function y(L)=f(x(L))
That syntax is not valid. The left side of the "=" in a function statement must be one of:
- A single named plain variable with no indexing at all (including no structure references)
- [] around a list of named plain variables variables with no indexing at all (including no structure references). Commas are recommended between the variable names, but spaces are also accepted. Semi-colons are not accepted
- The special name varargout may be used as the left side by itself, or as the last entry in a list
The right side of the "=" in a function statement must be one of:
- a single named plain variable with no indexing at all (including no structure references) that acts as the function name
- function name as above followed by () with nothing in the ()
- function name as above followed by () and inside the () a comma-separated list of named plain variables with no indexing at all (including no structure references).
- Inside the () the special name varargin may be used as the only entry, or as the last entry in a list
- Inside the () the special token ~ may be used in place of any entry, such as function f(~,x)
Your code is not valid syntax because it tries to index the output variable, and because inside the () list on the right, it uses something that is not a plain variable or ~
Sulaymon comments:
%clc; % no need
%clear all; % no need
function Y=f(x) % Must be
% The rest comes here
end
thanks for your comment but i got it. wrong was not in code, in my matlab version. İts okay now.
Thanks again :)
Your code had a few potential errs which are not MATLAB version problem.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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
- United Kingdom (English)

