How ı can write this function on Matlab

 Accepted Answer

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 Comments

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.
Rik
Rik on 4 Jun 2020
Edited: Rik on 4 Jun 2020
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!