I have two equations:
y1 = 2sinx1;
y2 = 2cos^2(x1) + 3sin(2x2+3);
here y1 = 0 while y2 = 1.
Can anyone please tell me which approach would be the best to find out the values of x1 and x2.
Thank you.

 채택된 답변

Torsten
Torsten 2016년 3월 31일
편집: Torsten 2016년 3월 31일

0 개 추천

Try this:
function driver
y=zeros(1,2);
y(1)=0;
y(2)=1;
x0=zeros(1,2);
x=fsolve(@(x)HW1(x,y),x0)
function res = HW1(x,y)
res(1)=y(1)-2*sin(x(1));
res(2)=y(2)-(2*cos(x(1))^2 + 3*sin(2*x(2) + 3));
Best wishes
Torsten.

댓글 수: 2

Muhammad Umar Farooq
Muhammad Umar Farooq 2016년 3월 31일
Thanks a lot. It works perfectly well. I am really thankful to you.
I am new to Matlab. Can you tell me which optimization will let me find the unique values of x1 and x2 for the given values of y1 and y2. If I change the interval from [0 0] to [2 2] it finds the zeros between this interval.
Torsten
Torsten 2016년 3월 31일
Your equations don't have a unique solution. So - depending on the starting guess x0 - you'll get different solutions for x. Do you have any condition on x that could make the solution unique ?
Best wishes
Torsten.

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

추가 답변 (1개)

Vlad Miloserdov
Vlad Miloserdov 2016년 3월 30일

0 개 추천

if you still need this
A=solve('0 = 2*sin(x1)','1 = 2*cos(x1)^2 + 3*sin(2*x2+3)','x1','x2');
% first ans
A.x1(1)
A.x2(1)
% second ans
A.x1(2)
A.x2(2)

댓글 수: 5

Muhammad Umar Farooq
Muhammad Umar Farooq 2016년 3월 31일
Thank you for your answer.
Yes, It solves the equation but I want to do this using optimization. Maybe fzero or something. I made a function but when I use fzero or some other optimization method it gives the error of not enough input arguments.
Thank you.
Torsten
Torsten 2016년 3월 31일
You have two equations - thus you will need MATLAB's "fsolve".
Best wishes
Torsten.
Muhammad Umar Farooq
Muhammad Umar Farooq 2016년 3월 31일
Yes, Thank you.
I have tried to use fsolve when I input the function, I get an error of not enough input arguments and the optimization stops.
Torsten
Torsten 2016년 3월 31일
Please show your code.
Best wishes
Torsten.
Muhammad Umar Farooq
Muhammad Umar Farooq 2016년 3월 31일
function [y1,y2] = HW1(x1,x2)
y1 = 2*sin(x1);
y2 = 2*cos(x1)^2 + 3*sin(2*x2 + 3);
end
I call this function HW1 from optimization tool box and it stops after giving error of not enough input arguments.
Thank you for your help. I am stuck for two days with this error.

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

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

태그

질문:

2016년 3월 30일

댓글:

2016년 3월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by