Error: Function definitions are not permitted in this context

Hi, i tried multiple times to change the function inside a script, but it kept fail eventhough i follow all the instructions and keep correcting. Recently i knew that Matlab 2015b can't include function and script at the same window, so please give me solution. thank you
close all; clear; clc;
%menentukan opsi optimisasi
options = optimoptions('fminsearch','Display','iter','Algorithm','quasi-newton');
%menentukan guess value
xy_guess = [0,0];
%memanggil algoritma optimisasi
[xy_opt, fval] = fminsearch(@fun,xy_guess,options)
%fungsi objektif
function f = fun( in )
x = in(1);
y = in(2);
f = 100*(y-x^2)^2 + (1-x))^2
end

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2020년 3월 20일
Cut these code from your script. Create a separate .M file called fun.m and past the code.
%fungsi objektif
function f = fun( in )
x = in(1);
y = in(2);
f = 100*(y-x^2)^2 + (1-x))^2
end

댓글 수: 3

thank you. i'll try
i separated it but still error. what should i do next?
It must not be the same error. What is the error message?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품

릴리스

R2015b

질문:

2020년 3월 20일

댓글:

2020년 3월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by