您好,
1 如果您想用solve算子来得出交点值,您需要把您方程适中除自变量外所有的其它参数用数字的形式直接写出来,而不是继续用变量名!
请把您在注释% junction point T z 之后的语句替换为下面的语句 即可成功运行:
zR = solve('1/(000379-0000283log10(0101325+1030981(750+z)0000001))-27315-(22+0054z+2erfc(z/2sqrt(3E-7500)))','z');
tR = 1/(000379-0000283log10(0101325+1030981(750+zR)0000001))-27315;
zSol = double(zR);
tSol = double(tR);
str = sprintf('The intersection of 2 lines is : \n z = %f \n t = %f', zSol,tSol);
disp(str);
plot(tSol,zSol,'r+','LineWidth',3,'MarkerSize',13);
PS 如果您需要查看十分十分确切的结果。请直接在命令窗口(Command Window)中输入:zR 或者tR,便会显示其小数点后二十多位的数值结果。
zR = 1624976393478409132553141680058
tR = 11293566711605738946287734914997
2我看了您上面写的内容。感觉您应该是属于工程领域的吧?由于,我是搞工科的。所以这样的话,我觉得您并不需要使用solve算子来求出理论交点。即使求出来了,也没有太大的实际意义。往往我们需要的数值顶多在小数点后3~6位左右就差不多了。太深的数值精度对于工科而言毫无意义。
3基于上面的第二点,我修改了您的命令文档。然后通过赋予容忍度1e-5来确定交点。主要思想是:先重新定义z向量(在这里我是用了1e6个点来描述1~300区间内的z值,并将之存储为double双精度类型变量);重新计算T1与TB;用T1-TB得出residu ;然后对于residu中凡是绝对值 < 容忍度(1e-5)的项目都认为是等于0。由此,我们便可以得出一系列在理论交点附近的点。最后再对这些点求平均值,就可以得出您的交点坐标了。
4下面是我修改后的m文件。您直接复制粘贴后运行就行了。另外我额外做了一些图像处理并修改了一些您以前命令中的一些错误,希望能够帮助到您。
clear all
clc
% temperature diffusion 温度扩散曲线
z = 0:300; %depth in mbsf 深度
t1 = 500; % time
b = 0054; %thermal gradient
DT = 2; %temperature change
k = 3E-7; %thermal diffusivity
To = 22; %initial temperature
T1 = To+bz+DTerfc(z/2sqrt(kt1)); % 温度随深度扩散曲线
%plot temperature diffusion line
figure('Name', 'Diagram for wulude','NumberTitle','off');
hold on
title('The intersection of 2 lines','FontSize',12);
plot(T1,z,'g','LineWidth',2); %green
xlabel('Temperature (C)','Color','b','FontWeight','bold');
ylabel('Depth (mbsf)','Color','b','FontWeight','bold');
grid on
% hydrate stability Line 稳定方程曲线
% 335% salt water 100% pure methane
P = 0101325+1030981(750+z)0000001; %净水压力
LP = log10(P);
TB = 1/(000379-0000283LP)-27315; %稳定方程曲线
% plot hydrate equilibrium
plot(TB,z,'m--','LineWidth',2); %megenta dash
%到这里都没问题
% junction point T z 求两条曲线交点,下面这行出错。
tolerance = 1e-5;
% all the value which is smaller than this will be
% considered as 0
% here, in order to find a good result, we'll use a range more precise
% and recalculate the 2 functions
clear T1 TB z residu
z = linspace(1,300,1e6); % this time, we define 1e6 points between 1 and 300
T1 = To+bz+DTerfc(z/2sqrt(kt1));
P = 0101325+1030981(750+z)0000001; %净水压力
LP = log10(P);
TB = 1/(000379-0000283LP)-27315;
residu = T1 - TB; % the difference between 2 functions
% some post-traitement of the result
% therefore we can locate the intersection point
results = [];
for i = 1 : length(z)
if abs(residu(i)) < tolerance
results = [results,i];
end;
end;
l = length(results);
zResult = z(results(1):results(l));
tResult = T1(results(1):results(l));
zSol = mean(zResult);
tSol = mean(tResult);
% draw the point in the figure
plot(tSol,zSol,'r+','LineWidth',3,'MarkerSize',13);
disp('Reel Solution Found');
str = sprintf('The intersection of 2 lines \n z = %f t = %f',zSol,tSol);
disp(str);
title(str,'FontSize',12);
5最后,由于我电脑装的是32位系统,所以我的最多只能用1e6个点来描述1~300这个区间。如果您使用的是64为系统+64位MATLAB,您可以增大这个数值,并且相应的减小容忍度,来无限接近理论交点——如果您的确需要一个十分十分精确的交点坐标
相反,您可以通过减少点数,增大容忍度,来快速定位您的交点——特别是当如果您处理多条有多个交点的函数时,此方法能够节省很多时间。并且满足您所要求的精确度。
希望上述两种求解方法都能帮助到您~
很高兴为您解答~
1、我们利用MATLAB求下图双曲线方程x^2/4^2-y^2/3^2=1和直线方程y=1/2x+1的交点。 2、启动MATLAB,新建脚本(Ctrl+N),在脚本编辑区输入如图代码。 3、保存
//先判断两条线段是否不平行(最好同时判断是否有交点并且不平行,因为浮//点运算不精确),然后计算两条线段的交点。以下是C语言代码:
#include<stdioh>
#include<mathh>
#define eps 1e-8
#define zero(x) (((x)>0(x):-(x))<eps)
struct point{double x,y;};
//计算交叉乘积(P1-P0)x(P2-P0)
double xmult(point p1,point p2,point p0){
return (p1x-p0x)(p2y-p0y)-(p2x-p0x)(p1y-p0y);
}
//判点是否在线段上,包括端点
int dot_online_in(point p,point l1,point l2){
return zero(xmult(p,l1,l2))&&(l1x-px)(l2x-px)<eps&&(l1y-py)(l2y-py)<eps;
}
//判两点在线段同侧,点在线段上返回0
int same_side(point p1,point p2,point l1,point l2){
return xmult(l1,p1,l2)xmult(l1,p2,l2)>eps;
}
//判两直线平行
int parallel(point u1,point u2,point v1,point v2){
return zero((u1x-u2x)(v1y-v2y)-(v1x-v2x)(u1y-u2y));
}
//判三点共线
int dots_inline(point p1,point p2,point p3){
return zero(xmult(p1,p2,p3));
}
//判两线段相交,包括端点和部分重合
int intersect_in(point u1,point u2,point v1,point v2){
if (!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2))
return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);
}
//计算两线段交点,请判线段是否相交(同时还是要判断是否平行!)
point intersection(point u1,point u2,point v1,point v2){
point ret=u1;
double t=((u1x-v1x)(v1y-v2y)-(u1y-v1y)(v1x-v2x))
/((u1x-u2x)(v1y-v2y)-(u1y-u2y)(v1x-v2x));
retx+=(u2x-u1x)t;
rety+=(u2y-u1y)t;
return ret;
}
int main(void)
{
point u1,u2,v1,v2,ans;
printf("请输入线段1的两个端点:\n");
scanf("%lf%lf%lf%lf",&u1x,&u1y,&u2x,&u2y);
printf("请输入线段2的两个端点:\n");
scanf("%lf%lf%lf%lf",&v1x,&v1y,&v2x,&v2y);
if (parallel(u1,u2,v1,v2)||!intersect_in(u1,u2,v1,v2)){
printf("无交点!\n");
}
else{
ans=intersection(u1,u2,v1,v2);
printf("交点为:(%lf,%lf)",ansx,ansy);
}
return 0;
}
如果知道两曲线的数学形式,最好数学上解方程组,求得交点
如果不知道,或者方程组不好解,只能求得近似交点,用判断语句
如两个曲线的坐标数组分别是(x1,y1),(x1,y2)(这个需要知道两个函数的交点的x范围是x1,且x1的步长足够小):find(abs(y1-y2)<=eps)
再有,如果纯粹是一些数据画的图,横坐标不同即(x1,y1),(x2,y2)
可以数据拟合两条曲线,用多项式拟合或指数拟合,或其他拟合,拟合出函数形式,解方程。
在同一坐标内,分别用不同线型和颜色绘制曲线y1=02e-05xcos(4πx)和y2=2e-05xcos(πx),标记两曲线交叉点如题,绘图简单,关键是不知道怎么求交点并且标记?还请大神指教,多谢。
消去w: v - v³/3 + I = (v + a)/b
整理得v³ + 3(1/b - 1)v + 3(a/b - I) = 0
令p = 3(1/b - 1), q = 3(a/b - I), 原方程变为v³ + pv + q = 0
然后代入w = (v + a)/b可以得到w
以上就是关于帮忙matlab求两条曲线交点程序,不知问题出在哪里。全部的内容,包括:帮忙matlab求两条曲线交点程序,不知问题出在哪里。、如何用matlab求出两条函数曲线的交点,可以的话发个代码、用C语言制作一个求两条线段交点的程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)